views:

127

answers:

3

Is there a ready made lisp macro that allows chaining (piping) of functions? I couldn't find one. I'll try to explain what I mean with this example.

Instead of using let* with lots of unused intermediate variables like this:

(let*
  ((var1 (f1 x y))
   (var2 (f2 x var1))
   (var3 (f1 var2 z)))
 var3)

I would like to have it written like this:

(->
  (f1 x y)
  (f2 x _)
  (f1 _ z))

where, obviously _ will be return value from previous expression. A plus is if would be possible to use _1, _2, ... to reference previously returned values.

This is the idea, exact syntax is not that important.

I know this is not that hard to write, but seems so useful that it has to be written already.

+3  A: 

Why not just

(f1 (f2 x (f1 x y)) z)

?

Or make that into a function ?

Christoffer Hammarström
Well, the second approach seems more readable, especially with longer function sequences.
Dev er dev
actually I find this more understandable. With the piping version I try to reconstruct the flow of values in my mind. The version here makes it clearer. The piping version requires an 'imperative' mindset.
Rainer Joswig
+1  A: 

via On Lisp

grettke
+4  A: 
Vatine
Thanks. That's exactly what I was looking for.
Dev er dev