This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-08-12
Channels
- # bangalore-clj (4)
- # beginners (40)
- # boot (53)
- # cider (34)
- # cljs-dev (9)
- # cljsrn (11)
- # clojure (113)
- # clojure-boston (5)
- # clojure-dev (9)
- # clojure-dusseldorf (4)
- # clojure-russia (8)
- # clojure-spec (11)
- # clojure-uk (12)
- # clojurescript (88)
- # cloverage (17)
- # conf-proposals (4)
- # core-async (30)
- # cursive (9)
- # datomic (107)
- # euroclojure (5)
- # hoplon (196)
- # luminus (10)
- # off-topic (20)
- # om (24)
- # om-next (1)
- # onyx (80)
- # parinfer (3)
- # pedestal (16)
- # planck (44)
- # proton (8)
- # protorepl (1)
- # re-frame (19)
- # reagent (7)
- # spacemacs (2)
- # untangled (29)
- # yada (25)
@polymeris: you can't deref the promise value as is in cljs, because deref is a sync operation and in clj blocks until the promise is resolved, but in cljs, no blocking is posible, this is because in cljs you can't deref a promise
Additionally, clj promise is blocking and does not offers composability of the promise abstraction in js/cljs world. funcool/promesa is an attempt to create an uniform abstraction that works in very similar way in both platforms š
@scott.haleen: your macro is working against the expression passed as cfg
, not its result
@oahner: it works with a map is passed {:a 1}
but not a form (assoc {:a 1} :b 2)
how do I force it to evaluate the form if need?
A macro is a function that is called on code-as-data before that code is evaluated.
You could try using eval
but youāll likely run into bizarre behavior (trying to evaluate expressions in the context of the reader).
Iād ask: what are you really trying to do? You probably donāt need a macro.
I agree probably not, I was just trying to have a utility that is pretty much exactly like the sample. The idea being you can pull keys from a map and have it *auto* assigned to a variable. Opposed to (sample {:a 1} [:a] (fn [a] (identity a))
If it really is as simple as what you have above you can do (let [{:keys [a b]} cfg] a)
you could make a function or simple syntax-quote macro on top of destructuring which is essentially what you want.
Also I am just trying to understand this better.
My friend wrote this book, you shoudl check it out: http://www.braveclojure.com/writing-macros/
I have been using that as well, thanks
(defmacro sample [cfg keys form]
`(let [{:keys ~(mapv (comp symbol name) keys)} ~cfg] ~form))
Basically macros happen before your code evaluates. The map you pass in is just data. But the expression is not simple data. Its code in the form of data.
@seancorfield: just made it work
@seancorfield: awesome
Well, to be clear ~cfg
will put the literal value of cfg
there, not the "run" version.
The literal expression (assoc {:a 1} :b 2)
is dropped into the expansion:
boot.user=> (macroexpand-1 '(sample (assoc {:a 1} :b 2) [:a :b] a))
(clojure.core/let [{:keys [a b]} (assoc {:a 1} :b 2)] a)
(full macro expansion is confusing in this case)
In my original version why doesn't changing cfg
to ~cfg
work. I tried that or was at least along that path and got Attempting to call unbound fn: #'clojure.core/unquote
Because you did not use the backtick to quote the syntax expression.
~
only works inside a backtickād expression
ah ok
(well, thatās not quite true but close)
I find it much easier to write macros using the backtick and then ~
-"escaping" the bits I want evaluated.
basically the backtick allows you to return the expression itself instead of the evaluation of that expression
In real world Clojure code, macros are rarely needed ā we have about 33,000 lines of production Clojure and only 10 macros in all of that.
So theyāre good to learn and know but are only needed occasionally.
right, I already know I can do what I am trying to do with a function ... it is really syntactic sugar probably a little fragile to just auto let
the symbol
but i spent way to long trying to figure it out
Thanks!
Is there a sente channel around?
'fraid not. Iāve used Sente in the past so I can try to help (but itās been a fair while now).