I am trying to implement 'interleave' for myself but get a outOfMemoryError
my version should be same as:
(interleave [1 2] [3 4])
Here is the code:
(defn myInterleav [col1 col2]
(loop [r []
c1 (first col1)
c2 (first col2)]
(if (and c1 c2)
(recur (conj (conj r c1) c2)
(next col1)
(next col2))
r)))
(myInterleav [1 2] [3 4])
Version 2 as 'noisesmith' suggested to rebind args, but get nullPointer error.
(defn myIL2 [col1 col2]
(loop [m []
[e1 & c1] col1
[e2 & c2] col2]
(if (and e1 e2)
(recur (conj m e1 e2)
c1
c2)
m
)))
(myIL2 [1 2] [3 4]), I get[1 3 2 4].(flatten (map list col1 col2))do what you want - my thinking was that if you could take the corresponding items in each collection, join them in some way and then take the contents of the resulting collections as a whole, you'd get where you want to be. Almost certainly there's a better way, which I hope somebody will point out!(apply mapcat list colls)is how I would implement this, but that doesn't really address his code.