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

Enable multiple function composition in prefix form #33568

Merged
merged 16 commits into from
Oct 16, 2019
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 also supports composition of multiple functions: `∘(f, g, h) = f ∘ g ∘ h`
JeffFessler marked this conversation as resolved.
Show resolved Hide resolved
and splatting `∘(fs...)` for an iterable.
JeffFessler marked this conversation as resolved.
Show resolved Hide resolved

# 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