This project demonstrates a variety of of Clojure language features and library functions using factorial computation as an example.
Many Clojure tutorials (and CS textbooks, for that matter) use factorial computation to teach recursion. I implemented such a function in Clojure and thought: "why stop there?"
Approaches used to calculate factorials:
- Using
loop
andrecur
- Using
reduce
andrange
- Using
apply
andrange
- Using
apply
take
anditerate
- Using
reduce
andrange
but returning afn
(a "higher-order" factorial function) - Using
defmacro
to create a function that always calculates the same factorial. - Using
cons
range
andeval
- Parallel computation using
future
dosync
andalter
- Parallel computation using
ref
agent
send
andawait
- Parallel computation using
reduce
andpmap
- Extremely convoluted parallel computation using
reduce
andpvalues
(requiring a macro) - Yet more convoluted parallel computation using
reduce
andpcalls
(also requiring a macro) - By getting the
nth
value from alazy-seq
of factorials. - Using
trampoline
and mutually-recursive functions defined withletfn
. - Using
defmethod
defmulti
anddefrecord
(plusupdate-in
and->
) for recursive computation. - Using a Java primitive array and
areduce
- Using Java interop to call a Java class.
- Using Java interop, but with
->
to tame a goofy "Builder" pattern. - Using
reify
to implement a pre-existing Java interface using one of our previous functions. - Using
incanter.core/factorial
(duh!)
Buggy or just wrong functions that still "work."
- (Mis-)using
def
do
anddotimes
(defective code) - (Mis-)using
def
do
andwhile
(yet more defective code) - (Mis-)using
while
atom
andswap
(not precisesly defective, but not a good use of Atoms). - An attempt with
agent
and recursivesend-off
calls, which often fails due to a race condition withawait
This project builds with Leiningen 2. Note that it includes some Java sources (for the interop examples).
Running lein test
or lein repl
will compile said Java sources.
Also, the source code comments are formatted for Marginalia. Configure your
profile to use the lein-marginalia plugin and run lein marg
to generate documentation.
This project also has a GitHub page hosting the generated Marginalia documentation (with some manual tweaks). Point your browser to
http://noahlz.github.io/factorials
CI is hosted by travis-ci.org
Creative Commons CC0 1.0 Universal
See: http://creativecommons.org/publicdomain/zero/1.0/legalcode
Noah Zucker (nzucker at gmail.com / @noahlz)