Skip to content

Instantly share code, notes, and snippets.

@bsless
Created August 4, 2020 06:40
Show Gist options
  • Select an option

  • Save bsless/6f2029d830a1fb24f12e50f6cdd09441 to your computer and use it in GitHub Desktop.

Select an option

Save bsless/6f2029d830a1fb24f12e50f6cdd09441 to your computer and use it in GitHub Desktop.
sort of cons-ing of sequences
(require '[clojure.core.protocols :as p])
(deftype Concatenation [coll1 coll2]
clojure.lang.IReduceInit
(reduce [_ f init]
(reduce f coll2 (reduce f init coll1 )))
clojure.lang.Seqable
(seq [this] (concat coll1 coll2)))
(defn concatenation
([coll1] coll1)
([coll1 coll2] (Concatenation. coll1 coll2))
([coll1 coll2 coll3]
(-> coll1
(Concatenation. coll2)
(Concatenation. coll3)))
([coll1 coll2 coll3 & colls]
(loop [c (-> coll1
(Concatenation. coll2)
(Concatenation. coll3))
[coll & colls] colls]
(if coll
(Concatenation. c coll)
c))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment