My main question is, what tasks/apps is clojure ideally suited for?
I've been playing with implementing Binary Search Trees in clojure and
Java as a way to learn clojure and compare the two languages. My
original thought was to implement some basic data types and algorithms
in a handful of interesting languages and compare the experience. For
me, the list includes clojure, java, scala, and erlang.
So far, my experience implementing basic BST functionality in clojure
has felt contrived, and was definitely easier in java.
Code: https://github.com/ToddG/experimental/tree/master/clojure/algorithms
1. TreeNode
(defrecord TreeNode [left right key] ...
I played with writing this basic TreeNode, but it has the disadvantage
of requiring rebuilding the ancestor nodes back up to the root. It's
also not clear to me how to write various traversals
(inorder,postorder,preorder) for this.
A thread on stacktrace suggested that instead of writing one's own
implementation of a balanced tree, one could just use clojure's sorted
map. It's based on clojure.lang.PersistentTreeMap, a balanced
(red-black) tree implemented in java. Great idea, and in practice,
that's exactly what I'd do, just like in java land I'd use
java.util.TreeMap. But, I'm plodding along, b/c I want to see this
exercise through.
2. HashTree
(defrecord Node [key data] ...
(defrecord HashTree [table] ...
My next try was to create a BST based on clojure's hash-map
(clojure.lang.PersistentHashMap). Node and HashTree use calculated
values for the left and right child nodes rather than maintaining
pointers within the Node record.
Next up, I plan to try and extend my Traversal protocol to these record
types. I've already tried with TreeNode, and that stumped me:
(defprotocol TRAVERSAL (inorder [this tree visitor]) ...
At the end of the day, I'm not sure how valid this exercise is. I'm
certainly slowly learning clojure...but perhaps learning by way of what
not do do. At this point, I think I'd prefer to write interesting
algorithms/datatypes in java and then surface them to clojure.
BTW - I've documented the links I've found useful along the way in the
README.
I've ordered 'Land of Lisp', so I'll report back how that works w/ Clojure.
-Todd
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en