Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clojure for loop, store the values in a set or map

This one has been bothering me for a while now, How should we store a value in a set or map in a for loop?

(let [s #{}]
     (for [ i (range 10)
            j (range 10) ]
      (into s [i j])))

i know this will not work, but i want a functionality similar to this , where the set will finally contain [0 0] [0 1]...[0 9] [1 0]...[9 9]

Thanks

like image 344
KaKa Avatar asked Sep 29 '11 17:09

KaKa


1 Answers

If I understand your question correctly you need to turn your expression inside-out:

(let [s #{}]
  (into s (for [i (range 10) 
                j (range 10)] 
            [i j])))

The thing to realize here is that for returns a value (a lazy sequence) unlike for-loops in more imperative languages like Java and C.

like image 121
Jonas Avatar answered Sep 28 '22 13:09

Jonas