and to set $it?

For the Compleat Fan
Locked
xificurC
Posts: 5
Joined: Tue Jul 11, 2017 6:42 am

and to set $it?

Post by 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?

Code: Select all

(filter (fn (x) (> x 10))
        (map inc '(1 2 3 4)))
could then be written as

Code: Select all

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

Code: Select all

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

TedWalther
Posts: 608
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC
Contact:

Re: and to set $it?

Post by TedWalther »

$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:

Code: Select all

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

Code: Select all

(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
Posts: 608
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC
Contact:

Re: and to set $it?

Post by TedWalther »

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.

Locked