doall

104 views
Skip to first unread message

Renata Soares

unread,
May 16, 2018, 1:08:58 PM5/16/18
to Clojure
Hello,

I have this vec called inviteds [:2 :4]

and I want to produce something like (hash-map :2 1, :4 1)

(doall (map #(hash-map % 1) inviteds))

but returns ({:2 1} {:4 1})

How can I produce the same result as (hash-map :2 1, :4 1) with doall (or other similar function)?

Thanks!

Laurens Van Houtven

unread,
May 16, 2018, 1:12:51 PM5/16/18
to clo...@googlegroups.com
Hi Renada,


The magic function you're looking for is called "frequencies": https://clojuredocs.org/clojure.core/frequencies

You could look at its implementation if you want to know how to implement this :)


lvh

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Colin Yates

unread,
May 16, 2018, 1:13:38 PM5/16/18
to clo...@googlegroups.com
If you have a sequence of maps then merge is your friend:

(def maps = [{:2 1} {:4 1}])
(apply merge maps)

To create convert a sequence into a map whose key is always 1:
(into {} (map (fn [x] [x 1]) [:2 :4]))  

HTH

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to

For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.

Mauricio Aldazosa

unread,
May 16, 2018, 3:38:22 PM5/16/18
to clo...@googlegroups.com
Hi Renata,

The problem with your code is that it is using map at the top level, which is a function that returns a lazy sequence. You are asking clojure to do something like:

Take each number n in inviteds and create a sequence of hash maps, where each hash map has n as a key and 1 as it's value.

But what you really want is a function at the top level that returns a hash map. As Colin shows, you can use into (here is a slightly different way than the one he used):

Take each number in inviteds and create a sequence of pairs. Each pair should have the number as it's first value, and 1 as the second. Then take those pairs, and "pour" them into a hash map.

(->> inviteds
     (map (fn [number] (vector number 1)))
     (into {}))

You could also do it via reduce:

Start with an empty hash map, then assoc each number in inviteds with 1 as it's value:

(reduce (fn [m number] (assoc m number 1))
        {}
        inviteds)

Cheers,
Mauricio

Justin Smith

unread,
May 16, 2018, 3:42:12 PM5/16/18
to clo...@googlegroups.com
as an aside, :1 etc. are bad keywords (accepted by some readers and not others, technically not valid according to the docs), and usually the presence of keywords like that indicates over-eager keywordizing of json input, or a misunderstanding of clojure hash-maps

Sean Corfield

unread,
May 16, 2018, 4:15:22 PM5/16/18
to clo...@googlegroups.com

(zipmap inviteds (repeat 1))

 

Sean Corfield -- (970) FOR-SEAN -- (904) 302-SEAN
An Architect's View -- http://corfield.org/

"If you're not annoying somebody, you're not really alive."
-- Margaret Atwood

 


From: clo...@googlegroups.com <clo...@googlegroups.com> on behalf of Renata Soares <renata...@gmail.com>
Sent: Wednesday, May 16, 2018 10:08:58 AM
To: Clojure
Subject: doall
 
Reply all
Reply to author
Forward
0 new messages