Skip to content

Commit 978123f

Browse files
jw3126jonas-schulzetkf
authored
Expose ComposedFunction as a public API (#37517)
* document ComposedFunction * add ComposedFunction to News.md * Give more descriptive names to the fields of ComposedFunction * export ComposedFunction Co-authored-by: Jonas Schulze <jonas.schulze7@t-online.de> Co-authored-by: Takafumi Arakaki <aka.tkf@gmail.com>
1 parent 864582c commit 978123f

File tree

4 files changed

+43
-8
lines changed

4 files changed

+43
-8
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ Standard library changes
105105
* `RegexMatch` objects can now be probed for whether a named capture group exists within it through `haskey()` ([#36717]).
106106
* For consistency `haskey(r::RegexMatch, i::Integer)` has also been added and returns if the capture group for `i` exists ([#37300]).
107107
* A new standard library `TOML` has been added for parsing and printing [TOML files](https://toml.io) ([#37034]).
108+
* The composition operator `` now returns a `Base.ComposedFunction` instead of an anonymous function ([#37517]).
108109
* A new standard library `Downloads` has been added, which replaces the old `Base.download` function with `Downloads.download`, providing cross-platform, multi-protocol, in-process download functionality implemented with [libcurl](https://curl.haxx.se/libcurl/) ([#37340]).
109110
* The `Pkg.BinaryPlatforms` module has been moved into `Base` as `Base.BinaryPlatforms` and heavily reworked.
110111
Applications that want to be compatible with the old API should continue to import `Pkg.BinaryPlatforms`,

base/exports.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export
3838
ComplexF64,
3939
ComplexF32,
4040
ComplexF16,
41+
ComposedFunction,
4142
DenseMatrix,
4243
DenseVecOrMat,
4344
DenseVector,

base/operators.jl

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -871,26 +871,58 @@ julia> fs = [
871871
julia> ∘(fs...)(3)
872872
3.0
873873
```
874+
See also [`ComposedFunction`](@ref).
874875
"""
875876
function end
876877

877-
struct ComposedFunction{F,G} <: Function
878-
f::F
879-
g::G
880-
ComposedFunction{F, G}(f, g) where {F, G} = new{F, G}(f, g)
881-
ComposedFunction(f, g) = new{Core.Typeof(f),Core.Typeof(g)}(f, g)
878+
"""
879+
ComposedFunction{Outer,Inner} <: Function
880+
881+
Represents the composition of two callable objects `outer::Outer` and `inner::Inner`. That is
882+
```julia
883+
ComposedFunction(outer, inner)(args...; kw...) === outer(inner(args...; kw...))
884+
```
885+
The preferred way to construct instance of `ComposedFunction` is to use the composition operator [`∘`](@ref):
886+
```jldoctest
887+
julia> sin ∘ cos === ComposedFunction(sin, cos)
888+
true
889+
890+
julia> typeof(sin∘cos)
891+
ComposedFunction{typeof(sin), typeof(cos)}
892+
```
893+
The composed pieces are stored in the fields of `ComposedFunction` and can be retrieved as follows:
894+
```jldoctest
895+
julia> composition = sin ∘ cos
896+
sin ∘ cos
897+
898+
julia> composition.outer === sin
899+
true
900+
901+
julia> composition.inner === cos
902+
true
903+
```
904+
!!! compat "Julia 1.6"
905+
ComposedFunction requires at least Julia 1.6. In earlier versions `∘` returns an anonymous function instead.
906+
907+
See also [`∘`](@ref).
908+
"""
909+
struct ComposedFunction{O,I} <: Function
910+
outer::O
911+
inner::I
912+
ComposedFunction{O, I}(outer, inner) where {O, I} = new{O, I}(outer, inner)
913+
ComposedFunction(outer, inner) = new{Core.Typeof(outer),Core.Typeof(inner)}(outer, inner)
882914
end
883915

884-
(c::ComposedFunction)(x...) = c.f(c.g(x...))
916+
(c::ComposedFunction)(x...) = c.outer(c.inner(x...))
885917

886918
(f) = f
887919
(f, g) = ComposedFunction(f, g)
888920
(f, g, h...) = (f g, h...)
889921

890922
function show(io::IO, c::ComposedFunction)
891-
show(io, c.f)
923+
show(io, c.outer)
892924
print(io, "")
893-
show(io, c.g)
925+
show(io, c.inner)
894926
end
895927

896928
"""

doc/src/base/base.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ Base.invokelatest
237237
new
238238
Base.:(|>)
239239
Base.:(∘)
240+
Base.ComposedFunction
240241
```
241242

242243
## Syntax

0 commit comments

Comments
 (0)