Friday, October 31, 2008

Positional Partial Application for Clojure

Here is a quick little utility for positional-partial application in clojure, similar to cut from srfi 26 for Scheme, but a function, rather than syntax.

(map (plx cons 'head :_) '((1 2) (3 4) (5 6)))
;;=> ((head 1 2) (head 3 4) (head 5 6))
(map (plx list 'head :_ 'end) '( middle mean meat )) 
;;=> ((head middle end) (head mean end) (head meat end))

If for some reason you need to pass a :_ keyword to a function you can specify another "leave open" sigil of any value by adding it to the function call.

((plx :pass + :pass 1) 4)
;;=> 5

This code is available from my mercurial repository hosted at freeHg. Just:

hg clone http://freehg.org/u/VincentToups/clojure-libs/ 
And add that to your classpath. Then you can use it by

(clojure/ns my-code (:use toups.functional))
;;; Clojure Code