and to set $it?

For the Compleat Fan

and to set $it?

Postby xificurC » Wed Jul 12, 2017 3:10 pm

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))
xificurC
 
Posts: 5
Joined: Tue Jul 11, 2017 6:42 am

Re: and to set $it?

Postby TedWalther » Wed Jul 12, 2017 4:56 pm

$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: 602
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC

Re: and to set $it?

Postby TedWalther » Wed Jul 12, 2017 4:58 pm

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.
TedWalther
 
Posts: 602
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC


Return to Anything else we might add?

Who is online

Users browsing this forum: No registered users and 3 guests

cron