Advertisement
CLooker

Untitled

Jan 5th, 2018
2,574
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; Write a function which takes a sequence consisting of items with different types and splits them up into a set of homogeneous sub-sequences. The internal order of each sub-sequence should be maintained, but the sub-sequences themselves can be returned in any order (this is why 'set' is used in the test cases).
  2.  
  3. (fn organize-v2
  4.   [some-seq]
  5.   (map
  6.    (fn [item]
  7.      (into [] (reverse item)))
  8.    (vals
  9.     (loop [index 0
  10.            new-seq {}]
  11.       (if (< index (count some-seq))
  12.         (recur
  13.          (inc index)
  14.          (update
  15.           new-seq
  16.           (type (some-seq index))
  17.           (fn [item]
  18.             (or (= item []) (conj item (some-seq index))))))
  19.         new-seq)))))
  20.  
  21. (= (set (organize-v2 [1 :a 2 :b 3 :c])) #{[1 2 3] [:a :b :c]})
  22. (= (set (organize-v2 [:a "foo"  "bar" :b])) #{[:a :b] ["foo" "bar"]})
  23. (= (set (organize-v2 [[1 2] :a [3 4] 5 6 :b])) #{[[1 2] [3 4]] [:a :b] [5 6]})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement