Page 1 of 1

and to set $it?

Posted: Wed Jul 12, 2017 3:10 pm
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))

Re: and to set $it?

Posted: Wed Jul 12, 2017 4:56 pm
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.")

Re: and to set $it?

Posted: Wed Jul 12, 2017 4:58 pm
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.