Skip to content

Instantly share code, notes, and snippets.

@mnzk
Created March 31, 2012 09:57
Show Gist options
  • Save mnzk/2261407 to your computer and use it in GitHub Desktop.
Save mnzk/2261407 to your computer and use it in GitHub Desktop.
mapn.clj
(defn mapn
([n f coll]
(->> (partition n coll)
(map (partial apply f))))
([n m f coll]
(->> (partition n m coll)
(map (partial apply f))))
([n m k f coll]
(->> (partition n m k coll)
(map (partial apply f)))))
(def mapncat
#(mapcat identity (apply mapn %&)))
;; examples
(def zeros (repeat 0))
(mapn 3 list [1 2 3 4 5 6 7 8]) ;=> ((1 2 3) (4 5 6))
(mapn 3 1 list [1 2 3 4 5 6 7 8]) ;=> ((1 2 3) (2 3 4) (3 4 5) (4 5 6) (5 6 7) (6 7 8))
(mapn 3 2 zeros list [1 2 3 4 5 6 7 8]) ;=> ((1 2 3) (3 4 5) (5 6 7) (7 8 0))
(mapn 3 + [1 2 3 4 5 6 7 8]) ;=> (6 15)
(mapn 3 1 + [1 2 3 4 5 6 7 8]) ;=> (6 9 12 15 18 21)
(mapn 3 2 zeros + [1 2 3 4 5 6 7 8]) ;=> (6 12 18 15)
(mapncat 2 1 zeros list [1 2 3 4 5 6 7 8]) ;=> (1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment