Skip to content

Instantly share code, notes, and snippets.

@kohyama
Last active October 6, 2015 00:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kohyama/e98d2c8a7b050765e20b to your computer and use it in GitHub Desktop.
Save kohyama/e98d2c8a7b050765e20b to your computer and use it in GitHub Desktop.
Divide a sequence into continuous subsequences
(def sss
(memoize
(fn [s]
(let [n (count s)]
(cond
(zero? n) '(())
(= n 1) `((~s))
:else (mapcat
(fn [c]
(let [[a b] (split-at c s)]
(map #(cons a %) (lazy-seq (sss b)))))
(range 1 (inc n))))))))
@kohyama
Copy link
Author

kohyama commented Oct 6, 2015

example to use

user=> (sss [1 2 3 4])
(((1) (2) (3) (4)) ((1) (2) (3 4)) ((1) (2 3) (4)) ((1) (2 3 4)) ((1 2) (3) (4)) ((1 2) (3 4)) ((1 2 3) (4)) ((1 2 3 4)))
user=> (sss [:a :b :c :d])
(((:a) (:b) (:c) (:d)) ((:a) (:b) (:c :d)) ((:a) (:b :c) (:d)) ((:a) (:b :c :d)) ((:a :b) (:c) (:d)) ((:a :b) (:c :d)) ((:a :b :c) (:d)) ((:a :b :c :d)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment