Friday, December 25, 2009

How to write a Clojure reader macro, part 2

This is a follow up to my earlier post about writing a reader macro. Here's the disclaimer again.


DISCLAIMER


I Completely agree with Rich's decision to NOT support reader macros. In an activity as simple as writing this post, I found many, many places to make a mistake the hard way. This is an extremely difficult activity to get right, and the end result is something that is not quite the same as normal Clojure. Use the following information at your own risk.



Okay, now that that's been said, let's get on with it. We're going to write a multi-character delimited reader macro this time. Since I'm a point free junkie, we're going to use partial for our example.

Here's what the final use case is going to be

user=>#[+ 1]
#< core$partial ...>

Let's start modifying LispReader.java again. The first thing we're going to do is insert a static symbol

//Inserted at line 40
static Symbol INTERPOLATE_S = Symbol.create("clojure.core", "partial");

Now, Let's take a look around line 84. You'll see the following entry in the array

macros['#'] = new DispatchReader();

The # character is bound to a DispatchReader. This is the object closure uses to implement multiple character reader macros (ever notice that they all start with #?). You'll also notice that there is a dispatchMacros array with several entires in it. Add the following entry

dispatchMacros['['] = new PartialReader();

We also need to define the PartialReader class. It is based on the VectorReader class, which can be found around line 994.



The heavy lifting is done by the readDelimitedList method. Note that the closing delimiter needs to be provided, and the recursive flag should be set to true. It returns an IPersistentList object. The only thing that needs to be done is to prepend a partial to the list. That is why the cons method is used to add a partial symbol (you still need classic macro-fu).

We've added everything we need to add to LispReader.java. All that's left to do is recompile clojure.jar and test the results

user=>(map #[+ 1] [1 2 3])
(2 3 4)

Of course, now that I think about it, comment might be a better symbol to use than partial...

That's how you add a delimited reader macro. Next time we'll look at creating a new dispatch character, and properly escaping everything.

Thursday, December 17, 2009

Proposal for 1.2 : New Seq Utilities

Time to brainstorm everyone!

I've been trying to come up with new sequence functions. Of course, there's nothing really new about these at all. They just aren't in core or c.c.seq-utils. Do any of these belong in contrib or core?

alternate [pred coll]
returns an alternating collection. Similar to a regex partition.

split [pred coll]
returns a collection of collection, split by matching the predicate.

take-last [n coll]
The mirror of drop-last

take-until [pred coll]
Returns a lazy sequence of successive items from coll while (pred item) returns false. pred must be free of side-effects.

drop-until [pred coll]
Returns a lazy sequence of the items in coll starting from the first item for which (pred item) returns true.

rotate [n coll]
Take a collection and left rotates it n steps. If n is negative, the collection is rotated right. Executes in O(n) time.

rotate-while [pred coll]
Rotates a collection left while (pred item) is true. Will return a unrotated sequence if (pred item) is never true. Executes in O(n) time.

rotate-until [pred coll]
Rotates a collection left while (pred item) is nil. Will return a unrotated sequence if (pred item) is always true. Executes in O(n) time.

Anyway, I define all of these functions and more here:


Do others have ideas for missing seq utilities?

Tuesday, December 15, 2009

How to write a Clojure reader macro

This is an article on how to write a basic reader macro in Clojure.


DISCLAIMER


I Completely agree with Rich's decision to NOT support reader macros. In an activity as simple as writing this post, I found many, many places to make a mistake the hard way. This is an extremely difficult activity to get right, and the end result is something that is not quite the same as normal Clojure. Use the following information at your own risk.


The first thing to identify is a behavior that you would like to have a reader macro for. For our example, I am going to use a modified form of Chas Emerick's amazing string interpolation macro. You can find his original article here. I took a modified version of his code, and placed it in core.clj (Be sure to create a new git branch). The code I used is below



Now that the desired functionality is in core, it is time to modify the reader. In this case we need to modify the file LispReader.java. I defined a static variable INTERPOLATE_S, and I am going to assign it the "|" reader macro.

//Inserted at line 40
static Symbol INTERPOLATE_S = Symbol.create("clojure.core", "interpolate-s");

Now, in order for this to be found we need to make an entry in the macros array. This can be done like so:

//Inserted at line 86
macros['|'] = new WrappingReader(INTERPOLATE_S);

The WrappingReader class takes a Symbol object, and wraps it around the next form that is read. Recall how the following form

@a-ref

is expanded to (deref a-ref). In our case

|"A string ~(+ 2 2)"

will be expanded to

(interpolate-s "A string ~(+ 2 2)")

Let's rebuild clojure.jar and try this out at a REPL.



As you can see this works just like Chas' macro. There are still a few things that need to be covered, such as:

* How to create a multiple character reader macro
* How to create a delimited reader macro

These will be topics for another day.

Tuesday, December 8, 2009

uses for take/drop-while

I've found a use for take-while & drop-while in a map.

To start let's define the following

user=>(defn sort-map [m] (apply sorted-map (apply concat m)))

That's right. A function to cast hash-map to tree-maps.

user=>(sort-map {3 :a 1 :c 2 :b})
{1 :c, 2 :b, 3 :a}

Why would I do that? Because I now have a way of making subsets based on the keys.

user=>(keys-pred take-while #(< 2 %) (sort-map {3 :a 1 :c 2 :b}))
{1 :c}

user=>(keys-pred drop-while #(< 2 %) (sort-map {3 :a 1 :c 2 :b}))
{2 :b, 3 :a}

So, when would this have an application?




Let's define the following map



And there you go, a use for take/drop while with maps :)