Fork me on GitHub
#clojure
<
2023-03-25
>
Joseph Graham09:03:01

why does map behave differently with a list than a vector?

(def listofmaps [{:name "Dave" :age 30} {:name "Jill" :age 28}])
#'user/listofmaps
(map :name listofmaps)
("Dave" "Jill")
user=>
vs:
(def listofmaps ({:name "Dave" :age 30} {:name "Jill" :age 28}))
#'user/listofmaps
(map :name listofmaps)
()
user=>

borkdude09:03:22

@U03KMPFU4TH The second listofmaps is not a list of maps, but a function call of the first map on the second map

borkdude09:03:41

You should either construct the list with (list {} {}) or quote it: '({} {})

2
Joseph Graham09:03:53

k no wonder I was confused

Joseph Graham09:03:07

makes sense now, thanks

👍 6
teodorlu12:03:49

A trick I often use is to look at intermediate values:

user=> [{:name "Dave" :age 30} {:name "Jill" :age 28}]
[{:name "Dave", :age 30} {:name "Jill", :age 28}]
user=> ({:name "Dave" :age 30} {:name "Jill" :age 28})
nil

Joseph Graham10:03:56

found some very odd behaviour with anonymous functions macro:

((fn [a b] [a b]) "foo" "bar")
["foo" "bar"]
(#([%1 %2]) "foo" "bar")
Execution error (ArityException) at user/eval316$fn (REPL:1).
Wrong number of args (0) passed to: clojure.lang.PersistentVector
user=>

Joseph Graham10:03:58

ah this "gotcha" is actually listed on the docs https://clojure.org/guides/learn/functions#_gotcha

👋 2
👍 4
p-himik10:03:09

Given this and the previous post, #beginners might be a better channel. ;) The first item in the #(...) form is called as a function.