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

predicate function negation with ComposedFunction #44752

Merged
merged 7 commits into from
Apr 12, 2022
Merged
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
Prev Previous commit
Next Next commit
revise show methods for ComposedFunction
  • Loading branch information
markmbaum authored and vtjnash committed Apr 7, 2022
commit 66614fc2a741c5d5cf2ed113e4f03bf28fab26eb
25 changes: 14 additions & 11 deletions base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1035,20 +1035,25 @@ end
∘(f, g, h...) = ∘(f ∘ g, h...)

function show(io::IO, c::ComposedFunction)
if c.outer |> Symbol |> Base.isoperator
print(io, '(', c.outer, ')')
else
show(io, c.outer)
end
_showcomposed(io, c.outer)
markmbaum marked this conversation as resolved.
Show resolved Hide resolved
print(io, " ∘ ")
show(io, c.inner)
_showcomposed(io, c.inner)
end

#shows !f instead of (!) ∘ f when ! is the outermost function
function show(io::IO, c::ComposedFunction{typeof(!)})
print(io, "!")
show(io, c.inner)
print(io, '!')
_showcomposed(io, c.inner)
end

_showcomposed(io::IO, x) = show(io, x)
#display operators like + and - inside parens
_showcomposed(io::IO, f::Function) = isoperator(Symbol(f)) ? (print(io, '('); show(io, f); print(io, ')')) : show(io, f)
#nesting for chained composition
_showcomposed(io::IO, f::ComposedFunction) = (print(io, '('); show(io, f); print(io, ')'))
#no nesting when ! is the outer function in a composition chain
_showcomposed(io::IO, f::ComposedFunction{typeof(!)}) = (print(io, '!'); show(io, f.inner))
stevengj marked this conversation as resolved.
Show resolved Hide resolved
markmbaum marked this conversation as resolved.
Show resolved Hide resolved

"""
!f::Function

Expand All @@ -1070,9 +1075,7 @@ julia> filter(!isletter, str)
```
"""
!(f::Function) = (!) ∘ f

#allows !!f === f
!(f::ComposedFunction{typeof(!)}) = f.inner
!(f::ComposedFunction{typeof(!)}) = f.inner #allows !!f === f
StefanKarpinski marked this conversation as resolved.
Show resolved Hide resolved

"""
Fix1(f, x)
Expand Down
14 changes: 14 additions & 0 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,20 @@ end
show(io::IO, f::Function) = show_function(io, f, get(io, :compact, false)::Bool)
print(io::IO, f::Function) = show_function(io, f, true)

function show_function(io::IO, c::ComposedFunction, compact::Bool)
markmbaum marked this conversation as resolved.
Show resolved Hide resolved
if compact
show(io, c)
else
print(io, "ComposedFunction(")
show_function(io, c.outer, false)
print(io, ", ")
show_function(io, c.inner, false)
print(io, ')')
end
end

show_function(io::IO, x, ::Bool) = show(io, x)

function show(io::IO, f::Core.IntrinsicFunction)
if !(get(io, :compact, false)::Bool)
print(io, "Core.Intrinsics.")
Expand Down
4 changes: 4 additions & 0 deletions test/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ end
@test filter(!isuppercase, str) == replace(str, r"[A-Z]" => "")
@test filter(!islowercase, str) == replace(str, r"[a-z]" => "")
@test !!isnan === isnan
markmbaum marked this conversation as resolved.
Show resolved Hide resolved
@test repr(!isnan) == "!isnan"
@test repr((-) ∘ sin) == "(-) ∘ sin"
@test repr(cos ∘ (sin ∘ tan)) == "cos ∘ (sin ∘ tan)"
markmbaum marked this conversation as resolved.
Show resolved Hide resolved
@test repr(!(cos ∘ !sin)) == "!(cos ∘ !sin)"
end

# issue #19891
Expand Down