# Help me understand pipes - strange behavior

**URL:** <https://discourse.julialang.org/t/help-me-understand-pipes-strange-behavior/102224>\
**Category:** New to Julia\
**Tags:** pipes\
**Created:** [July 29, 2023, 4:02am UTC](https://discourse.julialang.org/t/help-me-understand-pipes-strange-behavior/102224 "2023-07-29T04:02:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![andromodon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andromodon/32/36457_2.png) [@andromodon](https://discourse.julialang.org/u/andromodon)\
**Post date:** [July 29, 2023, 4:02am UTC](https://discourse.julialang.org/t/help-me-understand-pipes-strange-behavior/102224/1 "2023-07-29T04:02:59Z")

</div>

I’m running into something really strange for me in a pipeline. Can someone please explain why these two produce different results?

```julia
julia> "0X" |> x -> collect(UInt8, x) .|> x -> x == UInt8('X') ? UInt8(':') : x .|> x -> x - UInt8('0')
2-element Vector{UInt8}:
 0x00
 0x3a

julia> ("0X" |> x -> collect(UInt8, x) .|> x -> x == UInt8('X') ? UInt8(':') : x ) .|> x -> x - UInt8('0')
2-element Vector{UInt8}:
 0x00
 0x0a

```

All I did differently was add parenthesis in the second one and then it magically “works”. But to me the first one should “work” too, but it doesn’t. What’s happening and what should I keep in mind so I don’t get bit by this again?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [July 29, 2023, 4:33am UTC](https://discourse.julialang.org/t/help-me-understand-pipes-strange-behavior/102224/2 "2023-07-29T04:33:01Z")

</div>

You’re just confused by precedence of operators, try to run one pipe at a time and see when parentheses made a difference, and then you will figure out which operator has the precedence you didn’t expect

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [July 29, 2023, 5:08am UTC](https://discourse.julialang.org/t/help-me-understand-pipes-strange-behavior/102224/3 "2023-07-29T05:08:15Z")

</div>

```julia
julia> 1 |> x -> x==1 ? 'a' : 'b' |> x -> 'c'
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)

julia> 1 |> (x -> x==1 ? 'a' : 'b') |> x -> 'c'
'c': ASCII/Unicode U+0063 (category Ll: Letter, lowercase)

julia> 1 |> x -> x==1 ? 'a' : ('b' |> x -> 'c')
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)

```

basically what you intended is 2nd line, but 1st line is actually equivalent to 3rd line

---

<div class="post-metadata">

**Author:** ![andromodon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andromodon/32/36457_2.png) [@andromodon](https://discourse.julialang.org/u/andromodon)\
**Post date:** [July 29, 2023, 5:55am UTC](https://discourse.julialang.org/t/help-me-understand-pipes-strange-behavior/102224/4 "2023-07-29T05:55:34Z")

</div>

Thank you for your help, @jling. That is really confusing. I would expect pipes to work the same way as in bash. If you do `some stuff | other stuff` you almost always get `(some stuff) | (other stuff)`. `some (stuff | other) stuff` doesn’t make any sense. Why would julia not make the pipes parse last?

---

<div class="post-metadata">

**Author:** ![andromodon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andromodon/32/36457_2.png) [@andromodon](https://discourse.julialang.org/u/andromodon)\
**Post date:** [July 29, 2023, 6:00am UTC](https://discourse.julialang.org/t/help-me-understand-pipes-strange-behavior/102224/5 "2023-07-29T06:00:22Z")

</div>

Does one have to defensively wrap everything in () when using |\> or .|\> or just ternary operations?

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [July 29, 2023, 6:59am UTC](https://discourse.julialang.org/t/help-me-understand-pipes-strange-behavior/102224/6 "2023-07-29T06:59:28Z")

</div>

> [@andromodon](#):
>
> I would expect pipes to work the same way as in bash

Bash doesn’t have anonymous functions, which is where things are breaking down.

> [@andromodon](#):
>
> Why would julia not make the pipes parse last?

Because you can pipe inside functions too, and if that’s not what you intend, you should use some syntax to delineate where an anonymous function starts and ends. Bash has `{}` for that, here you can use parentheses. Anonymous functions also have the `function` block syntax with `end`, though 1-liners need a newline semicolon if you’re returning an expression inside parentheses or brackets e.g. `function (x); (x,) end`.

```julia
julia> "0X" |> (x -> collect(UInt8, x)) .|> (x -> x == UInt8('X') ? UInt8(':') : x) .|> (x -> x - UInt8('0'))
2-element Vector{UInt8}:
 0x00
 0x0a

julia> "0X" |> function (x) collect(UInt8, x) end .|> function (x) x == UInt8('X') ? UInt8(':') : x end .|> function (x) x - UInt8('0') end
2-element Vector{UInt8}:
 0x00
 0x0a

```

---

<div class="post-metadata">

**Author:** ![andromodon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andromodon/32/36457_2.png) [@andromodon](https://discourse.julialang.org/u/andromodon)\
**Post date:** [July 29, 2023, 9:44am UTC](https://discourse.julialang.org/t/help-me-understand-pipes-strange-behavior/102224/7 "2023-07-29T09:44:07Z")

</div>

Thanks @Benny . 🙏
