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 1 commit
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
74 changes: 68 additions & 6 deletions base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -339,18 +339,80 @@ 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.
By default, a function equivalent to `(x...) -> f(g(x...))` is returned, but
this may be specialized to create any functionally-equivalent, callable object.
"""
∘(f, g) = (x...)->f(g(x...))
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`, equal to
`(x...) -> !f(x...)`.
Returns a new function which applies boolean not to the output of `f`,
equivalent to `(x...) -> !f(x...)`.
"""
!(f::Function) = (x...)->!f(x...)
function !(f::Function)
Copy link
Member

Choose a reason for hiding this comment

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

Can't you just define this as ! ∘ f to avoid repeating the same machinery as above?

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 would like to, and I tried that first, but I got some kind of recursion when I tried to compile Julia. I didn't have time to investigate the cause of that yet, so I just used copy-paste.

Copy link
Contributor

@TotalVerb TotalVerb Jun 30, 2016

Choose a reason for hiding this comment

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

I'm pretty sure that's because ! ∘ f doesn't parse correctly. We probably need (!) ∘ f. This is a little ugly, which makes me like !f syntax a little more.

Copy link
Member Author

@andyferris andyferris Jun 30, 2016

Choose a reason for hiding this comment

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

That worked... thanks!

# Avoids splatting penalty. TODO: remove when that is fixed.
# Chose to implement up to length 16 (current setting of MAX_TUPLE_SPLAT)
negated() = !(f())
negated(x1) = !(f(x1))
negated(x1,x2) = !(f(x1,x2))
negated(x1,x2,x3) = !(f(x1,x2,x3))
negated(x1,x2,x3,x4) = !(f(x1,x2,x3,x4))
negated(x1,x2,x3,x4,x5) = !(f(x1,x2,x3,x4,x5))
negated(x1,x2,x3,x4,x5,x6) = !(f(x1,x2,x3,x4,x5,x6))
negated(x1,x2,x3,x4,x5,x6,x7) = !(f(x1,x2,x3,x4,x5,x6,x7))
negated(x1,x2,x3,x4,x5,x6,x7,x8) = !(f(x1,x2,x3,x4,x5,x6,x7,x8))
negated(x1,x2,x3,x4,x5,x6,x7,x8,x9) = !(f(x1,x2,x3,x4,x5,x6,x7,x8,x9))
negated(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10) =
!(f(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10))
negated(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11) =
!(f(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11))
negated(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12) =
!(f(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12))
negated(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13) =
!(f(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13))
negated(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14) =
!(f(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14))
negated(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15) =
!(f(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15))
negated(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16) =
!(f(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16))

negated(xs...) = !(f(xs...))

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

# array shape rules

Expand Down
4 changes: 2 additions & 2 deletions doc/stdlib/base.rst
Original file line number Diff line number Diff line change
Expand Up @@ -674,13 +674,13 @@ Generic Functions

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.
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``\ , equal to ``(x...) -> !f(x...)``\ .
Returns a new function which applies boolean not to the output of ``f``\ , equivalent to ``(x...) -> !f(x...)``\ .

Syntax
------
Expand Down