# \[RFC\] PipelessPipes.jl (now Chain.jl)

**URL:** <https://discourse.julialang.org/t/rfc-pipelesspipes-jl-now-chain-jl/50448>\
**Category:** Package Announcements\
**Created:** [November 19, 2020, 4:25pm UTC](https://discourse.julialang.org/t/rfc-pipelesspipes-jl-now-chain-jl/50448 "2020-11-19T16:25:38Z")\
**Posts on this page:** 20\
**Page:** 3

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [November 23, 2020, 6:16pm UTC](https://discourse.julialang.org/t/rfc-pipelesspipes-jl-now-chain-jl/50448/41 "2020-11-23T18:16:59Z")

</div>

Definitely it should be [@K](https://en.wikipedia.org/wiki/SKI_combinator_calculus), because it is a [kestrel](https://en.wikipedia.org/wiki/To_Mock_a_Mockingbird) bird!

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [November 23, 2020, 6:37pm UTC](https://discourse.julialang.org/t/rfc-pipelesspipes-jl-now-chain-jl/50448/42 "2020-11-23T18:37:22Z")

</div>

One thing I would also like in my dream chain function is a way to alter a global state without breaking the chain

```julia
@chain df begin 
    fun1(1)
    @eval_in_local_scope begin
    x = 5
    end
    fun2(x)
end

```

I’m sure this makes some anti-chain purists have their head explode, but it’s something I’ve always wanted in R.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [November 23, 2020, 7:05pm UTC](https://discourse.julialang.org/t/rfc-pipelesspipes-jl-now-chain-jl/50448/43 "2020-11-23T19:05:40Z")

</div>

That already works 🙂 the whole expression is just one big let block

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [November 23, 2020, 7:18pm UTC](https://discourse.julialang.org/t/rfc-pipelesspipes-jl-now-chain-jl/50448/44 "2020-11-23T19:18:15Z")

</div>

Can you give an example? is it just that expressions of the form `Expr(:call, ....)` are replaced and other ones are not?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [November 23, 2020, 7:22pm UTC](https://discourse.julialang.org/t/rfc-pipelesspipes-jl-now-chain-jl/50448/45 "2020-11-23T19:22:28Z")

</div>

Check the Readme, it lists the different ways expressions are replaced. You can also try it with macroexpand. Every expression just gets prepended with a `newvar = `, that’s why error highlighting works etc. So you can do anything you can do in normal code

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [November 23, 2020, 7:26pm UTC](https://discourse.julialang.org/t/rfc-pipelesspipes-jl-now-chain-jl/50448/46 "2020-11-23T19:26:12Z")

</div>

Ah okay, this is what people are discussing above about `@!` and `@aside`. ftw. I like `@aside`.

---

<div class="post-metadata">

**Author:** ![merlin](https://avatars.discourse-cdn.com/v4/letter/m/8baadc/32.png) [@merlin](https://discourse.julialang.org/u/merlin)\
**Post date:** [November 23, 2020, 7:28pm UTC](https://discourse.julialang.org/t/rfc-pipelesspipes-jl-now-chain-jl/50448/47 "2020-11-23T19:28:09Z")

</div>

I love it. In my work the data transform stages often stack pretty deep, so the pipe symbol at the end (though cool generally) become burdensome.

- Agree with eliminate `_`, good idea to make default with explicit `_` allowed first.
- Error handling – this is huge improvement
- `@!` for debugging flag: not a good choice, semantically overlaps with the mutating function syntax (and means kinda the opposite).
  - other options (really anything but `!`):
  - `@tee`, `@x` (for exclude), `@bypass`, `@()`, `@0`, `@<`

- The `begin` seems superflous, except that an `end` is needed, is it sensible to swallow that as well?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [November 23, 2020, 7:32pm UTC](https://discourse.julialang.org/t/rfc-pipelesspipes-jl-now-chain-jl/50448/48 "2020-11-23T19:32:17Z")

</div>

+1 for `@bypass` I like that too.

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [November 23, 2020, 7:55pm UTC](https://discourse.julialang.org/t/rfc-pipelesspipes-jl-now-chain-jl/50448/49 "2020-11-23T19:55:34Z")

</div>

> [@merlin](#):
>
> The `begin` seems superflous, except that an `end` is needed, is it sensible to swallow that as well?

The `begin`-`end` block is needed so that multiple statements are parsed as a single expression. If you have something like this

```julia
@chain df
    transform
end

```

it won’t parse correctly. The parser will assume that the macro call ends after `df`, due to the subsequent newline character.

---

<div class="post-metadata">

**Author:** ![dmolina](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmolina/32/5246_2.png) [@dmolina](https://discourse.julialang.org/u/dmolina)\
**Post date:** [November 23, 2020, 8:09pm UTC](https://discourse.julialang.org/t/rfc-pipelesspipes-jl-now-chain-jl/50448/50 "2020-11-23T20:09:33Z")

</div>

I agree in renaming @!, it is not easy to read, and it could confuse. For me @aside and @bypass are both nice and they are not confusing at all.

---

<div class="post-metadata">

**Author:** ![liuyxpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liuyxpp/32/9870_2.png) [@liuyxpp](https://discourse.julialang.org/u/liuyxpp)\
**Post date:** [November 24, 2020, 2:45am UTC](https://discourse.julialang.org/t/rfc-pipelesspipes-jl-now-chain-jl/50448/51 "2020-11-24T02:45:27Z")

</div>

+1 for `@bypass`

---

<div class="post-metadata">

**Author:** ![merlin](https://avatars.discourse-cdn.com/v4/letter/m/8baadc/32.png) [@merlin](https://discourse.julialang.org/u/merlin)\
**Post date:** [November 24, 2020, 4:10am UTC](https://discourse.julialang.org/t/rfc-pipelesspipes-jl-now-chain-jl/50448/52 "2020-11-24T04:10:41Z")

</div>

I’m suggesting the @chain macro absorb the ‘begin’ keyword, the parsed result would still contain it.

There are plusses and minuses to this, basically users have to treat the `@chain` macro as the beginning of a block.

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [November 24, 2020, 4:57am UTC](https://discourse.julialang.org/t/rfc-pipelesspipes-jl-now-chain-jl/50448/53 "2020-11-24T04:57:55Z")

</div>

> [@merlin](#):
>
> I’m suggesting the @chain macro absorb the ‘begin’ keyword, the parsed result would still contain it.

Parsing happens before macro expansion, so that’s not actually possible. In other words, you can’t use arbitrary syntax in a macro. You can only use syntax that the Julia parser knows how to parse.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [November 24, 2020, 11:14am UTC](https://discourse.julialang.org/t/rfc-pipelesspipes-jl-now-chain-jl/50448/54 "2020-11-24T11:14:50Z")

</div>

I’ve decided to use `@aside`, I think it both describes its purpose best and is easiest to understand without additional knowledge

---

<div class="post-metadata">

**Author:** ![qsong](https://avatars.discourse-cdn.com/v4/letter/q/d07c76/32.png) [@qsong](https://discourse.julialang.org/u/qsong)\
**Post date:** [December 3, 2020, 11:32am UTC](https://discourse.julialang.org/t/rfc-pipelesspipes-jl-now-chain-jl/50448/55 "2020-12-03T11:32:16Z")

</div>

As I like both Chain.jl and [Underscores.jl](https://github.com/c42f/Underscores.jl/), I did a [POC](https://github.com/ianqsong/Chain.jl/tree/poc) to combine the two.  
Borrow functions (and the name of `@_`) from [Underscores.jl](https://github.com/c42f/Underscores.jl/) and we can define anonymous functions in the pipe block as expressions of `_` or `_1,_2,...` (or `_₁,_₂,...`).

## Examples

The macro `@_` for POC is based on `@chian` and uses `__` instead of `_` as the placeholder.

```julia
@_ [1:5, 4:10] begin
  map(_[end]^2, __)
  filter(isodd, __)
end

using DataFrames
df = DataFrame(x = [1, 3, 2, 1], y = 1:4)
@_ df begin
    filter(_.x > 1 && isodd(_.y) , __)
    transform([:x, :y] => ByRow(_1 *100 + _2) => :z)
end

```

The original `@chain` would look like:

```julia
@chain [1:5, 4:10] begin
  map(x -> x[end]^2, _)
  filter(isodd, _)
end

@chain df begin
    filter(row -> row.x > 1 && isodd(row.y) , _)
    transform([:x, :y] => ByRow((a, b) -> a *100 + b) => :z)
end

```

---

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [March 24, 2021, 6:24am UTC](https://discourse.julialang.org/t/rfc-pipelesspipes-jl-now-chain-jl/50448/56 "2021-03-24T06:24:14Z")

</div>

I like the documentation example:

```julia
@chain df begin
  dropmissing
  filter(:id => >(6), _)
  groupby(:group)
  combine(:age => sum)
end

```

The only remaining underscore is in filter  
This could be removed as well using

`@where( :id .> 6 )`

Unfortunately you then have to vectorise the condition.  
Is there a way to avoid this and just have where( :id \> 6 ) ?

---

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [March 24, 2021, 6:39am UTC](https://discourse.julialang.org/t/rfc-pipelesspipes-jl-now-chain-jl/50448/57 "2021-03-24T06:39:33Z")

</div>

is there a Chain equivalent of vectorised piping eg:

`@pipe [1 2 3] .|\>  
log .|\>  
\_^2

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [March 24, 2021, 7:17am UTC](https://discourse.julialang.org/t/rfc-pipelesspipes-jl-now-chain-jl/50448/58 "2021-03-24T07:17:16Z")

</div>

No, that’s a trade off that comes from rewriting to temporary variables and keeping the expressions on each line otherwise intact. Also because there’s no symbol between lines that could signal this. That means broadcasting fusion does not happen across lines, but it’s not so common in the DataFrames scenario.

You can prefix function symbols like `@. log` though and of course use normal broadcasting like `_ .^ 2`, just remember there’s no fusion across lines.

---

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [March 25, 2021, 2:32am UTC](https://discourse.julialang.org/t/rfc-pipelesspipes-jl-now-chain-jl/50448/60 "2021-03-25T02:32:48Z")

</div>

In the below, the insertcols! function gives an error if the first argument \_ is removed.

```julia
@chain DataFrame(A=["a/b","c/d","x/y"]) begin
    insertcols!( _, ([:C1,:C2] .=> split.( _.A, '/' ) |> invert )...)
end

```

does the first argument removal feature not work for ! functions ?

Many thanks for your answer by the way.  
I’m asking all these dump questions because I love the package.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [March 25, 2021, 3:13am UTC](https://discourse.julialang.org/t/rfc-pipelesspipes-jl-now-chain-jl/50448/61 "2021-03-25T03:13:42Z")

</div>

> [@Lincoln\_Hannah](#):
>
> ```julia
> @chain DataFrame(A=["a/b","c/d","x/y"]) begin
> insertcols!( _, ([:C1,:C2] .=> split.( _.A, '/' ) |> invert )...)
> end
> 
> ```

This breaks because _whenever_ you have a `_` in the expression, the “first argument” rule ceases to apply. You always need the `_` in the correct places whenever you have any `_`.

[Previous page](https://discourse.julialang.org/t/rfc-pipelesspipes-jl-now-chain-jl/50448.md?page=2)

[Next page](https://discourse.julialang.org/t/rfc-pipelesspipes-jl-now-chain-jl/50448.md?page=4)
