Method chain in Julia

I read https://github.com/JuliaLang/julia/issues/5571 , which made me think that I could break such lines due to some comments:

a = [x*5 for x in 0:20 if x>4]

scale(y) = (x)-> y*x
filter(y) = x -> [z for z in x if z>y]

a|>(x->x/3)
    |>scale(2)
    |>filter(4)
    |>println

But I get the error:

ERROR: LoadError: syntax: "|>" is not a unary operator
 in include_from_node1(::String) at ./loading.jl:488
 in process_options(::Base.JLOptions) at ./client.jl:265
 in _start() at ./client.jl:321

Am I forced to use a|>(x->x/3)|>scale(2)|>filter(4)|>println?

+6
source share
1 answer

You can move statements |>to line ends:

julia> a|>(x->x/3)|>
       scale(2)|>
       filter(4)|>
       println

This syntax is that the parser must uniquely decide when the statement ends.

(in fact, I myself asked a question about such a problem and got a good answer. see Why is the `where` syntax in Julia sensitive to a new line? )

+10
source

All Articles