Skip to content

Instantly share code, notes, and snippets.

@luxbock
Created January 25, 2015 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save luxbock/99c509f409d8dff4d080 to your computer and use it in GitHub Desktop.
Save luxbock/99c509f409d8dff4d080 to your computer and use it in GitHub Desktop.
Referential Transparency
(filter even? (range 10))
;; equals
(new clojure.lang.LazySeq
(fn []
(when-let
[s (. clojure.lang.RT
(seq (new clojure.lang.LazySeq
#(let [b (clojure.lang.ChunkBuffer. 32)
comp (if (let [a (. clojure.lang.Numbers (isZero 1))]
(if a a (clojure.lang.Util/equiv 0 10)))
not=
(if (. clojure.lang.Numbers (isPos 1))
<
(if (. clojure.lang.Numbers (isNeg 1))
>
nil)))]
(loop* [i 0]
(if (let [a (< (clojure.lang.RT/count b) 32)]
(if a (comp i 10) a))
(do (.add b i)
(recur (. clojure.lang.Numbers (add i 1))))
(chunk-cons
(.chunk b)
(if (comp i 10) (do (range i 10 1))))))))))]
(if (instance? clojure.lang.IChunkedSeq s)
(let [c (.chunkedFirst s)
size (clojure.lang.RT/count c)
b (clojure.lang.ChunkBuffer. size)]
(let [xy (clojure.lang.RT/longCast size)]
(loop* [i 0]
(if (. clojure.lang.Numbers (lt i xy))
(do
(if (if (or (instance? Integer (.nth c i))
(instance? Long (.nth c i))
(instance? clojure.lang.BigInt (.nth c i))
(instance? BigInteger (.nth c i))
(instance? Short (.nth c i))
(instance? Byte (.nth c i)))
(. clojure.lang.Numbers
(isZero
(. clojure.lang.Numbers
and
(clojure.lang.RT/uncheckedLongCast (.nth c i))
1)))
(throw (IllegalArgumentException. (str "Argument must be an integer: " (.nth c i)))) )
(do (.add b (.nth c i))))
(recur (. clojure.lang.Numbers (unchecked_inc i)))))))
(chunk-cons (.chunk b) (filter even? (.chunkedMore s))))
(let [f (. clojure.lang.RT (first s))
r (. clojure.lang.RT (more s))]
(if (if (or (instance? Integer f)
(instance? Long f)
(instance? clojure.lang.BigInt f)
(instance? BigInteger f)
(instance? Short f)
(instance? Byte f))
(. clojure.lang.Numbers
(isZero
(. clojure.lang.Numbers
and
(clojure.lang.RT/uncheckedLongCast f)
1)))
(throw (IllegalArgumentException. (str "Argument must be an integer: " f))) )
(. clojure.lang.RT (cons f (filter even? r)))
(filter even? r)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment