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

add one arg function composition #34251

Merged
merged 7 commits into from
Jan 13, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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 NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ New library functions

New library features
--------------------
* Function composition now works also on one argument `∘(f) = f` (#34251)


Standard library changes
Expand Down
13 changes: 13 additions & 0 deletions base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,10 @@ and splatting `∘(fs...)` for composing an iterable collection of functions.
!!! compat "Julia 1.4"
Multiple function composition requires at least Julia 1.4.

!!! compat "Julia 1.5"
Composition of one function ∘(f) requires at least Julia 1.5.


# Examples
```jldoctest
julia> map(uppercase∘first, ["apple", "banana", "carrot"])
Expand All @@ -856,6 +860,15 @@ julia> ∘(fs...)(3)
3.0
```
"""
function ∘()
# Like +() and *() we leave ∘() undefined.
# While `∘() = identity` is a reasonable definition for functions, this
# would cause headaches for composition of user defined morphisms.
# See also #34251
msg = """Empty composition ∘() is undefined."""
Copy link
Member

Choose a reason for hiding this comment

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

Why define it at all then? Why not just let it be a method error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So that we don't get a stream of PRs that want to define it 😄

Copy link
Member

Choose a reason for hiding this comment

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

I think it's better to leave it undefined.

Copy link
Member

Choose a reason for hiding this comment

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

I see the benefit of making it very clear that not defining it is intentional with this approach. But I think leaving a comment is enough to prevent future PRs.

throw(ArgumentError(msg))
end
∘(f) = f
∘(f, g) = (x...)->f(g(x...))
∘(f, g, h...) = ∘(f ∘ g, h...)

Expand Down
18 changes: 18 additions & 0 deletions test/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,25 @@ Base.promote_rule(::Type{T19714}, ::Type{Int}) = T19714
@test ∘(x -> x-2, x -> x-3, x -> x+5)(7) == 7
fs = [x -> x[1:2], uppercase, lowercase]
@test ∘(fs...)("ABC") == "AB"

@test_throws ArgumentError ∘()
@test ∘(x -> (x, 1))(0) === (0, 1)
@test ∘(x -> (x, 2), x -> (x, 1))(0) === ((0, 1), 2)
@test ∘(x -> (x, 3), x -> (x, 2), x->(x,1))(0) === (((0, 1), 2), 3)
@test ∘(x -> (x, 4), x -> (x, 3), x->(x,2), x-> (x, 1))(0) === ((((0, 1), 2), 3), 4)

# test that user defined functors only need to overload the two arg version
struct FreeMagma
word
end
Base.:(∘)(a::FreeMagma, b::FreeMagma) = FreeMagma((a.word, b.word))

@test ∘(FreeMagma(1)) === FreeMagma(1)
@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))
end

@testset "function negation" begin
str = randstring(20)
@test filter(!isuppercase, str) == replace(str, r"[A-Z]" => "")
Expand Down