In Pipe.@pipe, |> followed by .|> Gives an error

|> followed by .|> Gives an error

@pipe 2 |>  _ .+ (1:3)                            #Works
@pipe       2 .+ (1:3)  .|>  _^2                  #Works
@pipe 2 |>  _ .+ (1:3)  .|>  _^2                  #Gives Error

Is there a way around this?
It works the other way round: .|> followed by |>
Example

@pipe (1:3)  .|>  _^2   |>   sum                  #Works
2 Likes
using MacroTools
MacroTools.prettify(@macroexpand @pipe 2 |>  _ .+ (1:3)  .|>  _^2 )

gives

:((2 |> 2 .+ (1:3)) .|> (mosquito->mosquito ^ 2))

but this works

@pipe 2 |>  _ .+ (1:3)  |>  _ .^ 2 

I think it’s just a parseing bug in Pipe.jl, so should raise an issue on github.

2 Likes

Many thanks xiaodai :slight_smile:

this is still giving an error. I haven’t raised an issue before. can you send a link to the place

1 Like

You can try @chain : GitHub - jkrumbiegel/Chain.jl: A Julia package for piping a value through a series of transformation expressions using a more convenient syntax than Julia's native piping functionality.

julia> @chain 2  _ .+ (1:3) map(_) do x x^2 end
3-element Vector{Int64}:
  9
 16
 25
1 Like

This thread is from 4.5 years ago, the landscape was quite different back then :slight_smile:
With DataPipes.jl, one can write the original example from the first post basically without any changes, and continue using .|>:

julia> @p 2 |> __ .+ (1:3) .|> __^2
3-element Vector{Int64}:
  9
 16
 25
or use `map` as you suggest
julia> @p 2 |> __ .+ (1:3) |> map(_^2)
3-element Vector{Int64}:
  9
 16
 25
1 Like

Nice!

And how do I do this?

julia> Pipe.@pipe """[{"a":1},{"a":2},{"a":3}]""" |> JSON.parse .|> _["a"]
3-element Vector{Int64}:
 1
 2
 3

julia> DataPipes.@pipe """[{"a":1},{"a":2},{"a":3}]""" |> JSON.parse .|> _["a"]
ERROR: syntax: all-underscore identifier used as rvalue around /home/hugo/.julia/packages/DataPipes/emgTy/src/pipe.jl:211

That’s

@p """[{"a":1},{"a":2},{"a":3}]""" |> JSON.parse .|> __["a"]

In DataPipes.jl, _ refers to the anonymous function argument, like @p 1:3 |> map(_ + 1). Use __ to refer to the previous step of the pipeline.

1 Like