Dominykas Mostauskis
Dominykas Mostauskis

Reputation: 8135

Clojure-esque threading macro?

How to implement, or is there an implementation of Clojure-like threading macros, namely thread-first (->) and thread-last (->>)?

Example:

# equivalent of sum(1, 2)
@thread-first 1 sum(2)

# equivalent of any(map(isequal(1), [1,2,3]))
@thread-last [1,2,3] map(isequal(1)) any

Upvotes: 1

Views: 171

Answers (1)

Bill
Bill

Reputation: 6086

Julia has pipelining, but in general the |> operator only allows one-argument functions. In Clojure, the thread-first and thread-last arguments insert the argument at the beginning or end of multiple arguments in the function.

You do have the @> and @>> macros in Lazy.jl:

https://github.com/MikeInnes/Lazy.jl#macros

These do thread-first and thread-last, but with different syntax. See the Lazy.jl docs. Example of thread-last:

 @>> 1:10 collect filter(isodd) square.() reduce(+)
 165

example of thread-first:

@> 6 div(2)
3

Upvotes: 2

Related Questions