Skip to content

Instantly share code, notes, and snippets.

@timsgardner
Last active August 29, 2015 14:10
Show Gist options
  • Save timsgardner/55e120d849ab312f8f45 to your computer and use it in GitHub Desktop.
Save timsgardner/55e120d849ab312f8f45 to your computer and use it in GitHub Desktop.
pleat
(defn screw-up-some-collections-1 [xs ys]
(reduce
(fn [acc1, x]
(conj acc1
(reduce
(fn [acc2, y]
(conj acc2 [x y]))
[]
ys)))
[]
xs))
(defn pleat
"reduce with the syntax flipped, if you're into that sort of thing"
([coll f]
(reduce f coll))
([coll init f]
(reduce f init coll)))
;; look this is nicer:
(defn screw-up-some-collections-2 [xs ys]
(pleat xs, [],
(fn [acc1, x]
(conj acc1
(pleat ys, []
(fn [acc2, y]
(conj acc2 [x y])))))))
;; => (= (screw-up-some-collections-1 (range 5) (range 5))
;; (screw-up-some-collections-2 (range 5) (range 5)))
;; true
;; => (pprint (screw-up-some-collections-2 (range 5) (range 5)))
;; [[[0 0] [0 1] [0 2] [0 3] [0 4]]
;; [[1 0] [1 1] [1 2] [1 3] [1 4]]
;; [[2 0] [2 1] [2 2] [2 3] [2 4]]
;; [[3 0] [3 1] [3 2] [3 3] [3 4]]
;; [[4 0] [4 1] [4 2] [4 3] [4 4]]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment