and to set $it?

Started by xificurC, July 12, 2017, 08:10:02 AM

Previous topic - Next topic

xificurC

Many lisps are taking over clojure's threading macros in some shape. Picolisp has a special variable @ that holds the result of the last computation of some functions (like newlisp's $it). and is one of them and it enables it to use it for threading. Would something like this be welcomed in newlisp?

(filter (fn (x) (> x 10))
        (map inc '(1 2 3 4)))

could then be written as

(and (map inc '(1 2 3 4))
     (filter (fn (x) (> x 10)) $it))

or even

(and '(1 2 3 4)
     (map inc $it)
     (filter (fn (x) (> x 10)) $it))

TedWalther

#1
$it has a slightly different meaning already.



But we could definitely use a "thrush" function. I was thinking, instead of two separate thrush operators -> and ->> like Clojure hash, I'd like a thrush like this:


(thrush initial-expr [idx] expr [idx] expr [idx] expr)

In this syntax, idx is an optional integer index, which thrush would use to know where in the argument list to put the previous expr in the next expr.  This gives full flexibility, without us having to change the API or do any contortions.  This works because thrus is supposed to compose functions; so if it comes across an argument that is an integer, then it knows to use it as an index.  Standard list indexing, -1 is the last item in the argument list, etc.  I think default to -1 is best, but once you set an index value, it stays the same until you set it again.


(thrush "hello" 1 printf)
(thrush "hello" 1 reverse (printf " world.")
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence.  Nine months later, they left with a baby named newLISP.  The women of the ivory towers wept and wailed.  \"Abomination!\" they cried.

TedWalther

#2
Haskell also has nice composition syntax with the "." operator, be worth studying that for some lessons.  Monads are such a big deal in Haskell, but I think a lot of JavaScript patterns are using monads and monadic style without even thinking about it.
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence.  Nine months later, they left with a baby named newLISP.  The women of the ivory towers wept and wailed.  \"Abomination!\" they cried.