Unflattening a sequence to sequences of repeating elements (clojure)
In Clojure, how do you partition a sequence to subsequences of repeating elements? E.g. :
[1 2 2 3 3 3 4 2 2 1 1 1]
to
[[1] [2 2] [3 3 3] [4] [2 2] [1 1 1]]
I've been playing around with some examples trying to understand clojure better, and was stuck on this开发者_如何学C one for some time.
user> (partition-by identity [1 2 2 3 3 3 4 2 2 1 1 1])
((1) (2 2) (3 3 3) (4) (2 2) (1 1 1))
user> (vec (map vec (partition-by identity [1 2 2 3 3 3 4 2 2 1 1 1])))
[[1] [2 2] [3 3 3] [4] [2 2] [1 1 1]]
(map (juxt count first) (partition-by identity [1 1 1 3 2 2]))
((3 1) (1 3) (2 2))
Three ones, then one three, followed by two twos!
精彩评论