Skip to content

Commit

Permalink
Added docstrings and tests for compositions
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Ferris committed Jun 29, 2016
1 parent dc8e1b5 commit 287c228
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
17 changes: 17 additions & 0 deletions base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,25 @@ eltype(x) = eltype(typeof(x))

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

"""
∘(f, g)
f ∘ g
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, an anonymous function `(x...) -> f(g(x))` is returned, but this may
be specialized to create any functionally-equivalent callable object.
"""
(f, g) = (x...)->f(g(x...))

"""
!(f::Function)
Applies boolean not to the output of `f`, return a new function
`(x...) -> !f(x...)` such that `(!f)(x...) = !(f(x...))`.
"""
!(f::Function) = (x...)->!f(x...)

# array shape rules
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
@test (!isless)(1,2) == false

0 comments on commit 287c228

Please sign in to comment.