Skip to content

Instantly share code, notes, and snippets.

@zfogg
Created June 27, 2013 01:18
Show Gist options
  • Save zfogg/5873244 to your computer and use it in GitHub Desktop.
Save zfogg/5873244 to your computer and use it in GitHub Desktop.
A lazy sequence of the Pythagorean Triples.
(defn t1
[[a b c]]
[($= 1 * a - 2 * b + 2 * c)
($= 2 * a - 1 * b + 2 * c)
($= 2 * a - 2 * b + 3 * c)])
(defn t2
[[a b c]]
[($= 1 * a + 2 * b + 2 * c)
($= 2 * a + 1 * b + 2 * c)
($= 2 * a + 2 * b + 3 * c)])
(defn t3
[[a b c]]
[($= -1 * a + 2 * b + 2 * c)
($= -2 * a + 1 * b + 2 * c)
($= -2 * a + 2 * b + 3 * c)])
(defn ts
[abc]
[(t1 abc) (t2 abc) (t3 abc)])
(defn lazy-mapcat
[f coll]
(lazy-seq
(if (not-empty coll)
(concat
(f (first coll))
(lazy-mapcat f (rest coll))))))
(def pythTrips
(lazy-cat [[3 4 5]] (lazy-mapcat ts pythTrips)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment