| Paste number 82957: | How to group/split contents of a collection into groups? |
| Pasted by: | AWizzArd |
| When: | 2 years, 7 months ago |
| Share: | Tweet this! | http://paste.lisp.org/+1S0D |
| Channel: | #clojure |
| Paste contents: |
;;; Nice would be something like
;;; (split-when (= % :x) [1 2 3 4 :x 5 6 7 8 :x 9 10 :x 11] ==>
;;; ((1 2 3 4) (:x) (5 6 7 8) (:x) (9 10) (:x) (11))
;;;
;;; How to do this easier than:
(use 'clojure.contrib.seq-utils)
(let [group (atom 1), old (atom true)]
(group-by #(if (= % :x)
(do
(reset! old false)
(swap! group inc))
(if @old
@group
(do
(reset! old true)
(swap! group inc))))
[1 2 3 4 :x 5 6 7 8 :x 9 10 :x 11]))
;;; ?
;;; While this is not exactly the same result as above
;;; but the data format comes close.This paste has no annotations.