This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-09-16
Channels
- # aleph (1)
- # aws (2)
- # bangalore-clj (2)
- # beginners (33)
- # cider (3)
- # cljs-dev (4)
- # cljs-experience (6)
- # cljsjs (1)
- # cljsrn (8)
- # clojure (84)
- # clojure-sanfrancisco (1)
- # clojure-spec (6)
- # clojure-uk (5)
- # clojurescript (42)
- # defnpodcast (3)
- # docs (3)
- # emacs (13)
- # events (1)
- # fulcro (2)
- # hoplon (19)
- # liberator (2)
- # off-topic (5)
- # onyx (7)
- # parinfer (1)
- # pedestal (1)
- # re-frame (13)
- # ring-swagger (13)
- # spacemacs (19)
- # yada (2)
QUESTION: Is there something that can be used from Clojure to create a Java class and instance at runtime? Like a proxy that doesn't need to extend from anything, and you can just define custom methods? Or like a macro that both defines a protocol and its implementation and reify it into an inctance all in one go?
gen-class can do this, but you'd need to do some trickery to meta-program a gen-class properly
I think this could give me what I want: https://github.com/jgpc42/insn
iirc gen-class literally checks for a flag like *compiling*
which is dynamic and you could override- worth trying at least?
the flag is *compile-files*
@didibus Depending on what you're trying to do, couldn't you use definterface
to declare whatever methods you want and just proxy
that interface with the methods defined?
yeah, that might actually be the simpler option
boot.user=> (definterface Greeter (^String greet []))
boot.user.Greeter
boot.user=> (proxy [Greeter] [] (greet [] "Hello"))
#object[boot.user.proxy$java.lang.Object$Greeter$150fd98b 0x6d9cee8a "boot.user.proxy$java.lang.Object$Greeter$150fd98b@6d9cee8a"]
boot.user=> (.greet *1)
"Hello"
proxy
acts as a proxy for Object
in the absence of a more specific class, and you can implement whatever methods you have interfaces for...
Ya, I guess that seems like the best route, could make a macro on top of them to do it all at once.
Any idea how I can have this return a meta:
(defmacro tm [s]
`(^void ~s))
(macroexpand-1 '(tm name))
@didibus emit a call to with-meta
(meta (vary-meta 'n assoc :tag `Integer))
this works, but not for the aliases like void (meta (vary-meta 'n assoc :tag `void))
tags are allowed to be strings - and I didn't say vary-meta, I said with-meta
but I guess vary-meta works, it's just more complex than you need
I guess, but vary-meta retains meta, with will lose the original meta. Which probably don't matter for what I'm doing here.
oh, right, because s is passed in, good point
checking...
also, your macroexpand doesn't show metadata, but if you bind *print-meta*
it can
user=> (binding [*print-meta* true] (prn ^:a {}))
^{:a true} {}
+user=> (binding [*print-meta* true] (prn '^void 'foo))
^{:tag void, :line 12, :column 36} (quote foo)
nil
+user=> (-> '^void foo meta :tag)
void
+user=> (-> '^void foo meta :tag type)
clojure.lang.Symbol
looks like it wants the symbol 'void
@carr0t cool, thanks for the info!
i have a question about fn definitions inside defrecord / deftype vs normal fns
they don’t seem to support :pre and :post conditions - is that right?
here are some worked examples
Maybe this a well known problem … in which case, OK … I’ll move along
But if I’m missing something, I would appreciate some tips / advice
Since it will be just a normal fn under the hood. When it s inline like in your example I think it s optimized and compiled differently
@mpenet I don’t have any luck when using extend
I was doing it wrong @mpenet… you are of course correct
inline it compiles to a method of the record (class), via extend it does a runtime lookup in the proto to find the corresponding clj fn that was registered
I lose access to x as I have to go via this
but we do get :post
conditions back too
thanks
input: [a b c d e] output: [ [ a b c d e], [ b c d e], [c d e], [d e] [e]] is there a builtin for this? (it's fine if it returns lists instead of vectors)
hi hivemind, I always see
:repositories [["releases" {:url ""
:creds :gpg}]]
anyone knows what the http://blueant.com does mean? it seems that is a unowned domain name.It's a William Gibson reference. https://en.wikipedia.org/wiki/Hubertus_Bigend
https://www.tor.com/2017/06/26/recognizing-a-familiar-future-william-gibsons-blue-ant-trilogy/
it's an example of using a private repository
I'm surprised they don't just use http://example.com, but it's used in the lein docs https://github.com/technomancy/leiningen/blob/master/doc/DEPLOY.md#artifactorynexusarchiva
@noisesmith thanks. I do agree that’s better to use http://example.com to avoid unnecessary confusion.
not that I know of, I would use (comp vec rseq vector)
leaving out vec if I don't strictly need a vector of course
anyway the rseq / vector combo is the only one that doesn't need to arrange the items twice
if we have varargs flip it would be (flip vector)
wait, where did -
come from?
Oh, just an example, I assumed he wanted that function to reverse arguments to a function. So I showed it in example
Any good reason why dissoc doesn't work on vectors? My only guess is because Clojure seems to default to not giving you any potentially inefficient collection functions.
that's pretty much it, the best option for an efficient collection that does this is a finger-tree
no, it's a decent library though, worked on by people who work on core https://github.com/clojure/data.finger-tree
Is there any way to access the form originally used to define a var, if that var was defined in the repl? eg if I do
(defn f [x] (inc x))
or (def a 3)
is there any way to recover that expression, given the name of the var? clojure.repl/source
will do it if it was defined in a source file, but I specifically want ones that were defined in the REPL.
I'm experimenting with doing it by creating a REPL using a custom :read
fn which saves off the forms, but I wanted to be sure there wasn't already a good existing solution.
if you look at what clojure.repl/source does, it uses the line number info and such to extract the source
there's no tracking of the actual input form that was compiled
you could make your own version of fn / def / defn etc. that does this
but ... yeah, nothing built in
That's what I suspected. I would find it useful to be able to say, "give me the def for this form, preceded by the defs for any locally-defined vars contained in the form." It would make it easier & less tedious to move code from the repl to a source file.
I'm having plenty of fun experimenting with a custom :read
fn for the repl, though 🙂
hi, can someone explain how to use client_middleware
in aleph
, so that i can do not parse the response body manually? (edited)