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

Function composition and negation, take 2 #17184

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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 @@ -256,6 +256,7 @@ export
~,
:,
=>,
∘,
A_ldiv_B!,
A_ldiv_Bc,
A_ldiv_Bt,
Expand Down
57 changes: 55 additions & 2 deletions base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,61 @@ eltype(::Type{Any}) = Any
eltype(t::DataType) = eltype(supertype(t))
eltype(x) = eltype(typeof(x))

# function pipelining
# function pipelining, composition & negation

|>(x, f) = f(x)

"""
∘(f, g)

Creates a composition of two functions (or functor objects), such that
Copy link
Contributor

Choose a reason for hiding this comment

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

this pr doesn't include functor objects any more, does it?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, it uses closures. But ∘ accepts functors (any callable) as arguments.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll edit it to make it clear.

`(f ∘ g)(x...) == f(g(x...))`. The `∘` symbol can be accessed at the REPL using
`\\circ`.

By default, a function equivalent to `(x...) -> f(g(x...))` is returned, but
this may be specialized to create any functionally-equivalent, callable object.
"""
function ∘(f, g)
# Avoids splatting penalty. TODO: remove when that is fixed.
# Chose to implement up to length 16 (current setting of MAX_TUPLE_SPLAT)
composed() = f(g())
composed(x1) = f(g(x1))
composed(x1,x2) = f(g(x1,x2))
composed(x1,x2,x3) = f(g(x1,x2,x3))
composed(x1,x2,x3,x4) = f(g(x1,x2,x3,x4))
composed(x1,x2,x3,x4,x5) = f(g(x1,x2,x3,x4,x5))
composed(x1,x2,x3,x4,x5,x6) = f(g(x1,x2,x3,x4,x5,x6))
composed(x1,x2,x3,x4,x5,x6,x7) = f(g(x1,x2,x3,x4,x5,x6,x7))
composed(x1,x2,x3,x4,x5,x6,x7,x8) = f(g(x1,x2,x3,x4,x5,x6,x7,x8))
composed(x1,x2,x3,x4,x5,x6,x7,x8,x9) = f(g(x1,x2,x3,x4,x5,x6,x7,x8,x9))
composed(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10) =
f(g(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10))
composed(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11) =
f(g(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11))
composed(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12) =
f(g(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12))
composed(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13) =
f(g(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13))
composed(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14) =
f(g(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14))
composed(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15) =
f(g(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15))
composed(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16) =
f(g(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16))

composed(xs...) = f(g(xs...))

return composed # This name is remembered and visible at the REPL
end

"""
!(f::Function)

Returns a new function which applies boolean not to the output of `f`,
equivalent to `(x...) -> !f(x...)`.
"""
!(f::Function) = (!) ∘ f

# array shape rules

function promote_shape(a::Tuple{Int,}, b::Tuple{Int,})
Expand Down Expand Up @@ -672,6 +724,7 @@ export
∪,
√,
∛,
∘,
colon,
hcat,
vcat,
Expand All @@ -685,6 +738,6 @@ import ..this_module: !, !=, $, %, .%, ÷, .÷, &, *, +, -, .!=, .+, .-, .*, ./,
.>=, .\, .^, /, //, <, <:, <<, <=, ==, >, >=, >>, .>>, .<<, >>>,
<|, |>, \, ^, |, ~, !==, ===, >:, colon, hcat, vcat, hvcat, getindex, setindex!,
transpose, ctranspose,
≥, ≤, ≠, .≥, .≤, .≠, ⋅, ×, ∈, ∉, ∋, ∌, ⊆, ⊈, ⊊, ∩, ∪, √, ∛
≥, ≤, ≠, .≥, .≤, .≠, ⋅, ×, ∈, ∉, ∋, ∌, ⊆, ⊈, ⊊, ∩, ∪, √, ∛, ∘

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

.. function:: ∘(f, g)

.. Docstring generated from Julia source

Creates a composition of two functions (or functor objects), such that ``(f ∘ g)(x...) == f(g(x...))``\ . The ``∘`` symbol can be accessed at the REPL using ``\circ``\ .

By default, a function equivalent to ``(x...) -> f(g(x...))`` is returned, but this may be specialized to create any functionally-equivalent, callable object.

.. function:: !(f::Function)

.. Docstring generated from Julia source

Returns a new function which applies boolean not to the output of ``f``\ , equivalent to ``(x...) -> !f(x...)``\ .

Syntax
------

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

@test ((x -> x+1) ∘ (x _> 2x))(5) == 11
Copy link
Contributor

Choose a reason for hiding this comment

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

typo @test ((x -> x+1) ∘ (x -> 2x))(5) == 11

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks

@test (!isless)(1,2) == false