Thanks for your contribution

It’s been fun looking through the clojure-contrib directories of the 1.1 distribution. There’s tons of really useful source code and examples. These are some of the things that I’ve noticed.

There is a contribution that makes it possible to do more advanced macrology. One thing that could be quite useful is symbol-macrolet, a means of having a symbol expand into a form at macroexpansion time.

user> (use ‘clojure.contrib.macro-utils)
user> (symbol-macrolet [x (print hello)] (let [hello 27] x))
user> (macroexpand ‘(symbol-macrolet [x (print hello)] (let [hello 27] x)))
(do (let* [hello 27] (print hello)))

There are some utilities that make it easier to work at the REPL. The source macro attempts to find the definition of a given symbol and print it out at the REPL.

user> (use ‘clojure.contrib.repl-utils)
user> (source use)
(defn use
  "Like ‘require, but also refers to each lib’s namespace using
  clojure.core/refer. Use :use in the ns macro in preference to calling
  this directly.

  ‘use accepts additional options in libspecs: :exclude, :only, :rename.
  The arguments and semantics for :exclude, :only, and :rename are the same
  as those documented for clojure.core/refer."
  [& args] (apply load-libs :require :use args))

There’s a library for defining and using monads. The monads defined by this library don’t look nearly as neat as definitions inside Haskell, which has syntactic sugar which support their use. However, this is a library that I’ll be playing with more in the future. The following example, which comes in the library examples, shows the use of a continuation monad that the library defines.

user> (use ‘clojure.contrib.monads)
user> (run-cont
  (domonad cont-m
    [x (m-result 1)
     y (m-result 2)]
    (+ x y)))
user> (def continuation nil)
user> (run-cont
  (domonad cont-m
    [x (m-result 1)
     y (call-cc (fn [c] (def continuation c) (c 2)))]
    (+ x y)))
user> (run-cont (continuation 5))

while we’re talking about Clojure, the language gets a good mention on the Herding Code podcast.

This entry was posted in Computers and Internet. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s