This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-05-20
Channels
- # beginners (49)
- # boot (139)
- # cider (10)
- # clojure (82)
- # clojure-belgium (59)
- # clojure-dusseldorf (5)
- # clojure-russia (11)
- # clojure-sanfrancisco (2)
- # clojure-uk (56)
- # clojurebridge (4)
- # clojurescript (138)
- # cursive (19)
- # datomic (8)
- # dirac (1)
- # editors (11)
- # emacs (18)
- # flambo (21)
- # hoplon (45)
- # jobs (1)
- # juxt (3)
- # keechma (1)
- # mount (43)
- # off-topic (2)
- # om (64)
- # om-next (1)
- # onyx (2)
- # other-languages (8)
- # re-frame (72)
- # reagent (99)
- # ring-swagger (7)
- # rum (3)
- # spacemacs (21)
- # specter (5)
- # untangled (42)
- # vim (4)
- # yada (7)
Thank you for the feedback @virmundi. I will give that a try
I'm trying to learn RabbitMQ in Clojure. When I try to get the default exchange binding, the repl locks up. If I do the same thing in a normal terminal repl, it's fine.
Hey, I’m trying to get a very simple macro to work but can’t seem to figure it out… I’m declaring my macro like this:
(defmacro print-stuff [to-print] `(println to-print))
and I’m doing (macroexpand '(print-stuff "Hello World”))
to inspect it, which returns (print-stuff "Hello World”)
indicating that something is wrong. Calling the macro with (print-stuff "Hello World”)
yields the error WARNING: Wrong number of args (1) passed to clojulator.ns/print-stuff at line 1 <cljs repl>
.
I’m sure I’m missing something super obvious...@victorbjelkholm: You are in ClojureScript, right? See https://github.com/clojure/clojurescript/wiki/Differences-from-Clojure#macros
Oooooh, yeah, that’s true, didn’t think about the macros working differently in clojurescript/clojure… Thanks a bunch @mfikes
@mfikes: yeah, trying it in the clojure repl and it works fine
@victorbjelkholm: Cool. You added ~
in front of to-print
, right?
@mfikes: nope, you need it for all the arguments?
yeah, apparently...
didn’t try executing it, just expanding the macro. But you’re right. Thanks for that heads-up
@victorbjelkholm: Yes, read up on how syntax-quote works. 🙂
Say I have a list of address books, some of them are of type “mail” an some of type “social” (both inside the same list), every mail account has a corresponding social where they share the key “alias", and I want to loop through every book of type mail, find it’s social book, and map them both into a new list paired as keys in a map. What is the most efficient / elegant way to do this? My current solution:
hmmm, something like this is probably better solved by specter https://github.com/nathanmarz/specter
it's a pain to traverse/update deeply nested data structures in pain clojure, spectre/traversy really help
another question, when doing an assoc-in db and passing it a map, it’s literally storing the map function in the db, how do you get it to store the evaluated map?
@mattsfrey: this is a common "don't" in Clojure - mixing side effects with lazy operations e.g. I/O and results of map. See this article: https://stuartsierra.com/2015/08/25/clojure-donts-lazy-effects
@agile_geek: very informative article, thanks
@mattsfrey: You're welcome. All of @stuartsierra's articles are worth a read.
another quick Q. Is this valid? Like will c actually be assigned from the result of b’s assignment? (I’m guessing not. and if not, what would be the best method for doing this?)
(map (fn [i] {:a i :b (do-some-calc i) :c (do-some-calc :b) }) items)
@mattsfrey: Put the value (do-some-calc i)
into a let
binding. Then use it twice (once to assign to :b
, once to do some more calculation)
i was thinking of that, the let statement would literally go inside the map function before the return?
@mattsfrey: In a map you are only going to have one value per key and each key is unique.
So you just look up the value using the key
i.e. in your example you can only have one :alias
key in your map
Ah OK
You could use drop-while
to drop the items in you list that don't include the required map and then take the first from the lazy sequence produced but it's not more efficient or really any more idiomatic than filter.
(def my-maps [{:fred "fred"} {:alias "alias1" :val1 "val1"} {:alias "alias1" :val2 "val2"}])
(first (drop-while (fn [m] (not= (:alias m) "alias1")) my-maps))
BTW your code should read:
(first (filter #(= (:alias %) "my-alias") abs))
@mattsfrey: Your other fn that needed the let binding would look something like this:
(map (fn [i] (let [b (do-some-calc i) c (do-some-calc b)] {:a i :b b :c c })) items)
You can't embed the let
in the middle of the map literal.Also the easiest way to work this stuff out is start a REPL and try it out.
BTW your filter thing could also be done using some
:
(some (fn [m] (when (= (:alias m) "my-alias") m)) abs)
It would be easier to 1. get the data. 2. Transform the data 3. put the new data back.
I would select things up like this `(group-by first (map (juxt :alias :val1 :val2) [{:alias 1 :val1 2} {:alias 1 :val2 7}])) `