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

Define g ⨟ f = f ∘ g #34832

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ export
:,
=>,
∘,
⨟,

# scalar math
@evalpoly,
Expand Down
19 changes: 19 additions & 0 deletions base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,7 @@ entered in the Julia REPL (and most editors, appropriately configured) by typing
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 composing an iterable collection of functions.
See also [`⨟`](@ref) for composition in the reversed order.

!!! compat "Julia 1.4"
Multiple function composition requires at least Julia 1.4.
Expand Down Expand Up @@ -864,6 +865,24 @@ function ∘ end
∘(f, g) = (x...)->f(g(x...))
∘(f, g, h...) = ∘(f ∘ g, h...)

"""
g ⨟ f

Compose functions in the "opposite order": i.e. `(g ⨟ f)(args...)` means `f(g(args...))`.
Composition `g ⨟ f` is defined as [`f ∘ g`](@ref ∘).

!!! compat "Julia 1.5"
Opposite composition `⨟` requires at least Julia 1.5.

# Examples
```jldoctest
julia> 1 |> (x -> 2x) ⨟ string
"2"
```
"""
function ⨟ end
⨟(fs...) = ∘(reverse(fs)...)

"""
!f::Function

Expand Down
1 change: 1 addition & 0 deletions doc/src/base/base.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ Base.invokelatest
new
Base.:(|>)
Base.:(∘)
Base.:(⨟)
```

## Syntax
Expand Down
5 changes: 5 additions & 0 deletions test/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ Base.promote_rule(::Type{T19714}, ::Type{Int}) = T19714
@test ∘(FreeMagma(1), FreeMagma(2)) === FreeMagma((1,2))
@test ∘(FreeMagma(1), FreeMagma(2), FreeMagma(3)) === FreeMagma(((1,2), 3))
@test ∘(FreeMagma(1), FreeMagma(2), FreeMagma(3), FreeMagma(4)) === FreeMagma((((1,2), 3), 4))

@test ⨟(FreeMagma(1)) === FreeMagma(1)
@test ⨟(FreeMagma(1), FreeMagma(2)) === FreeMagma((2,1))
@test ⨟(FreeMagma(1), FreeMagma(2), FreeMagma(3)) === FreeMagma(((3,2), 1))
@test ⨟(FreeMagma(1), FreeMagma(2), FreeMagma(3), FreeMagma(4)) === FreeMagma((((4,3), 2), 1))
end

@testset "function negation" begin
Expand Down