Fork me on GitHub
#beginners
<
2016-03-04
>
mull15:03:36

(defn some-func [{:keys [id name]}] (do-something id))
Is there a way to use the {:keys} shortcut but also have a reference to whatever map contained those keys?

urbanslug15:03:46

@mull do you know more about destructuring?

urbanslug15:03:55

I don't quite get your question.

mull15:03:03

@urbanslug: so I’d like to destructure some keys of a map in the function, but I’d also like to have a reference to the full map

mull15:03:21

s/reference/local binding(?)

urbanslug15:03:22

I believe using as will work

urbanslug15:03:35

[{:keys [id name]} as my-map]

urbanslug15:03:44

I could be wrong with the syntax let me check.

mull15:03:29

Syntax check passed simple_smile Thanks!

urbanslug15:03:24

You don't even need the as nice simple_smile

mull15:03:25

Hmm this seems to possibly work differently between let and defn

solicode15:03:31

Yeah, :as is needed when dealing with arguments to a function. For let though, you have the map on the right hand side, which is the difference

mull15:03:36

(defn show [{:keys [slug lang] :as university}] (str university)) <— this isn’t quite right. Would you mind correcting this?

solicode15:03:22

Looks fine to me

mull15:03:31

(show {:slug “bla” :lang “en” :something “else”)

mull15:03:45

Hm okay. REPLing

mull15:03:23

D’oh you’re right. Problem was further down in Hiccup land simple_smile Thanks to both of y’all!

lmergen18:03:51

ok, so, i feel stupid for asking this question, but i'm not understanding something

lmergen18:03:25

if i have a function such as if, and i want it to simply return an integer if it evaluates to true, at runtime it complains about not being able to convert an Integer to an IFun ... which means i'm probably missing something... consider this code:

(if (= jobcount remaining)
      (jobcount)
      (wait-for-job-results queue-url (- jobcount remaining)))))
that (jobcount) is where it goes wrong

Chris O’Donnell18:03:19

If you put something in parens, clojure will call it as a function. For example, if you try to evaluate (1) in the repl, it will throw the same error.

lmergen18:03:26

so i should get rid of the parens

lmergen18:03:34

oh geez ofcourse i'm stupid

Chris O’Donnell18:03:56

@lmergen: I'm quite sure I've made that mistake more than once. Don't feel too bad about it.

lmergen19:03:40

apparently, clojure gives me the need to add parens around everything, because otherwise things don't look right

Tim19:03:22

right, similar to js in a sense, like eatFood() with the parens invokes the function, whereas eatFood refers to the value

Tim19:03:31

but yeah, I make the same mistake all the time