Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
89 changes: 65 additions & 24 deletions src/MappedArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -226,38 +226,79 @@ function Base.showarg(io::IO, A::AbstractMappedArray{T,N}, toplevel=false) where
toplevel && print(io, " with eltype ", T)
end

function func_print(io, f, types)
ft = typeof(f)
mt = ft.name.mt
name = string(mt.name)
if startswith(name, '#')
# This is an anonymous function. See if it can be printed nicely
lwrds = code_lowered(f, types)
if length(lwrds) != 1
@static if VERSION >= v"1.12.0"
# for Julia 1.12 and later

function func_print(io, f, types)
ft = typeof(f)
name = string(Base.nameof(ft))
if startswith(name, '#')
# This is an anonymous function. See if it can be printed nicely
lwrds = code_lowered(f, types)
if length(lwrds) != 1
show(io, f)
return nothing
end
lwrd = lwrds[1]
c = lwrd.code
if length(c) == 2 && ((isa(c[2], Expr) && c[2].head === :return) || isa(c[2], Core.ReturnNode))
# This is a single-line anonymous function, we should handle it
s = lwrd.slotnames[2:end]
if length(s) == 1
print(io, s[1], "->")
else
print(io, tuple(s...), "->")
end
c1 = string(c[1])
for i = 1:length(s)
c1 = replace(c1, "_"*string(i+1)=>string(s[i]))
end
print(io, c1)
else
show(io, f)
end
else
show(io, f)
return nothing
end
lwrd = lwrds[1]
c = lwrd.code
if length(c) == 2 && ((isa(c[2], Expr) && c[2].head === :return) || (isdefined(Core, :ReturnNode) && isa(c[2], Core.ReturnNode)))
# This is a single-line anonymous function, we should handle it
s = lwrd.slotnames[2:end]
if length(s) == 1
print(io, s[1], "->")
else
print(io, tuple(s...), "->")
end

else
# for Julia pre-1.12

function func_print(io, f, types)
ft = typeof(f)
mt = ft.name.mt
name = string(mt.name)
if startswith(name, '#')
# This is an anonymous function. See if it can be printed nicely
lwrds = code_lowered(f, types)
if length(lwrds) != 1
show(io, f)
return nothing
end
c1 = string(c[1])
for i = 1:length(s)
c1 = replace(c1, "_"*string(i+1)=>string(s[i]))
lwrd = lwrds[1]
c = lwrd.code
if length(c) == 2 && ((isa(c[2], Expr) && c[2].head === :return) || (isdefined(Core, :ReturnNode) && isa(c[2], Core.ReturnNode)))
# This is a single-line anonymous function, we should handle it
s = lwrd.slotnames[2:end]
if length(s) == 1
print(io, s[1], "->")
else
print(io, tuple(s...), "->")
end
c1 = string(c[1])
for i = 1:length(s)
c1 = replace(c1, "_"*string(i+1)=>string(s[i]))
end
print(io, c1)
else
show(io, f)
end
print(io, c1)
else
show(io, f)
end
else
show(io, f)
end

end

eltypes(A::AbstractArray) = Tuple{eltype(A)}
Expand Down
15 changes: 13 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,24 @@ end
b = mappedarray(sqrt, a)
@test summary(b) == "4-element mappedarray(sqrt, ::$(Vector{Int})) with eltype Float64"
c = mappedarray(sqrt, x->x*x, a)
@test summary(c) == "4-element mappedarray(sqrt, x->x * x, ::$(Vector{Int})) with eltype Float64"
if VERSION >= v"1.12.0"
# Note: `[A-Za-z0-9_\"#]+` matches Julia symbols, including the special `var\"#21#22\"` syntax
summary_c = replace(summary(c), r"var\".*\"" => "var")
@test summary_c == "4-element mappedarray(sqrt, var(), ::$(Vector{Int})) with eltype Float64"
else
@test summary(c) == "4-element mappedarray(sqrt, x->x * x, ::$(Vector{Int})) with eltype Float64"
end
# issue #26
M = @inferred mappedarray((x1,x2)->x1+x2, a, a)
io = IOBuffer()
show(io, MIME("text/plain"), M)
str = String(take!(io))
@test occursin("x1 + x2", str)
if VERSION >= v"1.12.0"
# Test whether `str` contains a symbol name such as `var\"#23#24\"()`
@test occursin("var\"", str) && occursin("\"()", str)
else
@test occursin("x1 + x2", str)
end
end

@testset "eltype (issue #32)" begin
Expand Down