admin-announcements

Announcements from the Clojurians Admin Team (@U11BV7MTK @U077BEWNQ @U050TNB9F @U0ETXRFEW @U04V70XH6 @U8MJBRSR5 and others)
2015-11-24T18:42:48.000407Z

I'm hoping to find a cljs equivalent of Underscore's "indexBy" http://underscorejs.org/#indexBy . Would anyone be able to point me in the right direction?

roberto 2015-11-24T18:44:49.000408Z

group-by

roberto 2015-11-24T18:44:50.000409Z

?

2015-11-24T18:45:37.000410Z

given a list of json objects, if I group by a key (say: id), the value will be a list correct?

jstew 2015-11-24T18:47:53.000411Z

group-by returns a map.

jstew 2015-11-24T18:48:07.000412Z

and the values are lists, correct.

jstew 2015-11-24T18:48:32.000413Z

technically a vector.

2015-11-24T18:50:08.000414Z

ideally I would have (group-by :id items) => {:1 {:title "title here" :description "description here"} :2 {:title "another title" ....}

jaen 2015-11-24T18:50:29.000415Z

Maybe something like this then?

(into {} (map (juxt :age identity) [{:name "moe" :age 40} {:name "larry" :age 50} {:name "curly" :age 60}]))

jaen 2015-11-24T18:51:04.000416Z

juxt is a too clever way of saying "make me a vector of results of applying those functions to something"

2015-11-24T18:52:18.000417Z

that works perfectly! I'll have to look into juxt now

robert-stuttaford 2015-11-24T18:52:27.000418Z

juxt ftw

2015-11-24T18:52:36.000419Z

thanks @jaen @jstew

jaen 2015-11-24T18:52:51.000420Z

(that is in this case it is equivalent to (fn [map] [(:age map) (identity map)]))

robert-stuttaford 2015-11-24T18:53:09.000422Z

(map (juxt inc dec) [1 2 3]) ;; ([2 0] [3 1] [4 2])

jstew 2015-11-24T18:54:46.000424Z

That's clever. I need to use juxt more often.

jaen 2015-11-24T18:57:37.000427Z

That said, depending on how you use it later, you could just use group-by and just destructure on that, for example:

(for [[id [val & _]] (group-by :age [{:name "moe" :age 40} {:name "larry" :age 50} {:name "curly" :age 60}])]
      (println "id=" id "; val=" val))

jaen 2015-11-24T18:57:43.000428Z

So you won't have to write your own function then

2015-11-24T19:00:49.000429Z

looks like clojuredocs beat us to it http://clojuredocs.org/clojure.core/juxt#example-542692cfc026201cdc326e12

jstew 2015-11-24T19:02:21.000430Z

I noticed that but it's slightly different than the data structure that underscore returns

jaen 2015-11-24T19:02:40.000431Z

Swear I didn't see that before ; d

jstew 2015-11-24T19:03:25.000432Z

{:key {:map "map"}} vs {:key [{:map1 "map1"}]}

jaen 2015-11-24T19:05:11.000433Z

Does it? The first is indexBy, the second is groupBy.

jstew 2015-11-24T19:08:44.000435Z

Yep, you're right jaen. There is a groupBy that returns the same thing.