Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Extend the |> operator in order to chain tuples. #14476

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ copy(x::Union{Symbol,Number,AbstractString,Function,Tuple,LambdaStaticData,

# function pipelining
|>(x, f) = f(x)
|>(xs::Tuple, f) = f(xs...)

# array shape rules

Expand Down
11 changes: 11 additions & 0 deletions doc/stdlib/base.rst
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,17 @@ Generic Functions
julia> [1:5;] |> x->x.^2 |> sum |> inv
0.01818181818181818

.. function:: |>(xs::Tuple, f)

.. Docstring generated from Julia source
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no it isn't? docstrings need to be put in .jl code for this to be accurate

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry, I was away for a while, and haven't catch up to recent doc changes. I just copied it over. Where do I need to put this .jl code then, for this to be true?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oooh I get it now! :P

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's explained in CONTRIBUTING.md


Applies a function to the preceding tuple. This allows for easy function chaining.

.. doctest::

julia> (5, 2) |> divrem |> complex |> float
2.0 + 1.0im

.. function:: call(x, args...)

.. Docstring generated from Julia source
Expand Down
7 changes: 7 additions & 0 deletions test/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,10 @@ let xs = [[i:i+4;] for i in 1:10]
@test max(xs[1:n]...) == [n:n+4;]
end
end

# function chaining
f_chain(x) = :x
f_chain(xs...) = :xs
@test 1 |> f_chain == :x
@test (1, 2) |> f_chain == :xs
@test ((1, 2),) |> f_chain == :x