Page 1 of 1

Positional Partial application for Clojure

Posted: Fri Oct 31, 2008 7:19 am
by VincentToups
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.

Code: Select all

(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.

Code: Select all

((plx :pass + :pass 1) 4)
;;=> 5
This code is available from my mercurial repository hosted at freeHg. Just:

Code: Select all

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

Code: Select all

(clojure/ns my-code (:use toups.functional))
;;; Clojure Code
Maybe someone besides me will find it useful.

Re: Positional Partial application for Clojure

Posted: Fri Oct 31, 2008 11:47 am
by Unne
Aside from being able to specify your own symbol for the magic argument, how does this differ from the built-in:

Code: Select all

(map #(cons 'head %) '((1 2) (3 4) (5 6)))
(map #(list 'head % 'end) '(middle mean meat))

Re: Positional Partial application for Clojure

Posted: Fri Oct 31, 2008 11:54 am
by VincentToups
Well, it is a function.

Now that the # syntax is brought to my attention, this is sort of silly.

Re: Positional Partial application for Clojure

Posted: Fri Oct 31, 2008 12:43 pm
by Unne
No big deal, clearly someone else already thought it was a good idea. Some of the other libraries on your site look interesting. Have you considered contributing some to clojure-contrib?

Re: Positional Partial application for Clojure

Posted: Sat Nov 01, 2008 6:22 pm
by VincentToups
There really isn't that much I have to contribute but I have a large set of libraries in Scheme which would be nice to port.

Things like polynomial algebra and curve-fitting and so forth.