Chain.jl icon indicating copy to clipboard operation
Chain.jl copied to clipboard

Chain as pipe argument

Open knuesel opened this issue 4 years ago • 3 comments
trafficstars

Parentheses are necessary to put a chain in a pipe. For example the following

@chain DataFrame(a=1:3) begin select(:a) end |> display

fails with

ERROR: MethodError: no method matching select(::Symbol)
Closest candidates are:
  select(::AbstractDataFrame, ::Any...; copycols, renamecols) at ~/.julia/packages/DataFrames/vuMM8/src/abstractdataframe/selection.jl:940
  select(::Union{Function, Type}, ::AbstractDataFrame; renamecols) at ~/.julia/packages/DataFrames/vuMM8/src/abstractdataframe/selection.jl:943
  select(::Union{Function, Type}, ::GroupedDataFrame; copycols, keepkeys, ungroup, renamecols) at ~/.julia/packages/DataFrames/vuMM8/src/groupeddataframe/splitapplycombine.jl:711

Would it be possible to make this work without parentheses? Intuitively it seems that it should be possible since the chained code is already delimited by the begin ... end block.

knuesel avatar Nov 15 '21 09:11 knuesel

This seems a bit problematic because of the other @chain mode where you can do @chain x y z, then I'd have to separate normal |> calls from those after begin end and this sounds edge-casey to me..

jkrumbiegel avatar Dec 09 '21 14:12 jkrumbiegel

In your example you can of course put display after select

jkrumbiegel avatar Dec 09 '21 14:12 jkrumbiegel

Agreed, though I still think it would be nice if @chain blocks were composable with other pipeline code (mainly because for me a @chain block is often a logical unit, like in the example where putting display inside would work but is less clear I think than a processing block followed by some "unrelated" action).

knuesel avatar Dec 12 '21 10:12 knuesel