Skip to content

Enable multiple function composition in prefix form #33568

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

Merged
merged 16 commits into from
Oct 16, 2019
Merged
Changes from 9 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
17 changes: 16 additions & 1 deletion base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -827,17 +827,32 @@ julia> [1:5;] |> x->x.^2 |> sum |> inv
Compose functions: i.e. `(f ∘ g)(args...)` means `f(g(args...))`. The `∘` symbol can be
entered in the Julia REPL (and most editors, appropriately configured) by typing `\\circ<tab>`.

Function composition also works in prefix form: `∘(f, g)` is the same as `f ∘ g`.
The prefix form supports composition of multiple functions: `∘(f, g, h) = f ∘ g ∘ h`
and splatting `∘(fs...)` for an iterable.

# Examples
```jldoctest
julia> map(uppercase∘first, ["apple", "banana", "carrot"])
3-element Array{Char,1}:
'A'
'B'
'C'

julia> fs = [
x -> 2x
x -> x/2
x -> x-1
x -> x+1
];

julia> ∘(fs...)(3)
3.0
true
```
"""
∘(f, g) = (x...)->f(g(x...))

∘(f, g, h...) = ∘(f ∘ g, h...)

"""
!f::Function
Expand Down