Skip to content

Commit

Permalink
Merge pull request #348 from jishnub/oneelshow
Browse files Browse the repository at this point in the history
Pretty-printing for OneElement
  • Loading branch information
oxinabox authored Feb 2, 2024
2 parents 1788108 + 4d4173f commit b873ad1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/oneelement.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,10 @@ end
function broadcasted(::DefaultArrayStyle{N}, ::typeof(\), x::Number, r::OneElement{<:Any,N}) where {N}
OneElement(x \ r.val, r.ind, axes(r))
end

# show
_maybesize(t::Tuple{Base.OneTo{Int}, Vararg{Base.OneTo{Int}}}) = size.(t,1)
_maybesize(t) = t
Base.show(io::IO, A::OneElement) = print(io, OneElement, "(", A.val, ", ", A.ind, ", ", _maybesize(axes(A)), ")")
Base.show(io::IO, A::OneElement{<:Any,1,Tuple{Int},Tuple{Base.OneTo{Int}}}) =
print(io, OneElement, "(", A.val, ", ", A.ind[1], ", ", size(A,1), ")")
9 changes: 9 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2226,6 +2226,15 @@ end
@test n .\ v == n .\ w
end
end

@testset "show" begin
B = OneElement(2, (1, 2), (3, 4))
@test repr(B) == "OneElement(2, (1, 2), (3, 4))"
B = OneElement(2, 1, 3)
@test repr(B) == "OneElement(2, 1, 3)"
B = OneElement(2, (1, 2), (Base.IdentityUnitRange(1:1), Base.IdentityUnitRange(2:2)))
@test repr(B) == "OneElement(2, (1, 2), (Base.IdentityUnitRange(1:1), Base.IdentityUnitRange(2:2)))"
end
end

@testset "repeat" begin
Expand Down

0 comments on commit b873ad1

Please sign in to comment.