Skip to content

Commit

Permalink
Julia 1.7 fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman committed Apr 26, 2024
1 parent 03e46bf commit 5cab9cf
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 118 deletions.
120 changes: 2 additions & 118 deletions src/lib/OrdinalIndexing/src/OrdinalIndexing.jl
Original file line number Diff line number Diff line change
@@ -1,120 +1,4 @@
module OrdinalIndexing
struct One <: Integer end
const 𝟏 = One()
Base.convert(type::Type{<:Number}, ::One) = one(type)
Base.promote_rule(type1::Type{One}, type2::Type{<:Number}) = type2
Base.:(*)(x::One, y::One) = 𝟏

function Base.show(io::IO, ordinal::One)
return print(io, "𝟏")
end

struct OrdinalSuffixedInteger{T<:Integer} <: Integer
cardinal::T
function OrdinalSuffixedInteger{T}(cardinal::Integer) where {T<:Integer}
cardinal 0 || throw(ArgumentError("ordinal must be > 0"))
return new{T}(cardinal)
end
end
function OrdinalSuffixedInteger(cardinal::Integer)
return OrdinalSuffixedInteger{typeof(cardinal)}(cardinal)
end
function OrdinalSuffixedInteger{T}(ordinal::OrdinalSuffixedInteger) where {T<:Integer}
return OrdinalSuffixedInteger{T}(cardinal(ordinal))
end

cardinal(ordinal::OrdinalSuffixedInteger) = getfield(ordinal, :cardinal)
function cardinal_type(ordinal_type::Type{<:OrdinalSuffixedInteger})
return fieldtype(ordinal_type, :cardinal)
end

const th = OrdinalSuffixedInteger(𝟏)
const st = th
const nd = th
const rd = th

function Base.widen(ordinal_type::Type{<:OrdinalSuffixedInteger})
return OrdinalSuffixedInteger{widen(cardinal_type(ordinal_type))}
end

Base.Int(ordinal::OrdinalSuffixedInteger) = Int(cardinal(ordinal))

function Base.:(*)(a::OrdinalSuffixedInteger, b::Integer)
return OrdinalSuffixedInteger(cardinal(a) * b)
end
function Base.:(*)(a::Integer, b::OrdinalSuffixedInteger)
return OrdinalSuffixedInteger(a * cardinal(b))
end
function Base.:(:)(
start::OrdinalSuffixedInteger{T}, stop::OrdinalSuffixedInteger{T}
) where {T<:Integer}
return UnitRange{OrdinalSuffixedInteger{T}}(start, stop)
end

function Base.:(*)(a::OrdinalSuffixedInteger, b::OrdinalSuffixedInteger)
return (cardinal(a) * cardinal(b)) * th
end
function Base.:(+)(a::OrdinalSuffixedInteger, b::OrdinalSuffixedInteger)
return (cardinal(a) + cardinal(b)) * th
end
function Base.:(+)(a::OrdinalSuffixedInteger, b::Integer)
return a + b * th
end
function Base.:(+)(a::Integer, b::OrdinalSuffixedInteger)
return a * th + b
end
function Base.:(-)(a::OrdinalSuffixedInteger, b::OrdinalSuffixedInteger)
return (cardinal(a) - cardinal(b)) * th
end
function Base.:(-)(a::OrdinalSuffixedInteger, b::Integer)
return a - b * th
end
function Base.:(-)(a::Integer, b::OrdinalSuffixedInteger)
return a * th - b
end

function Base.:(:)(a::Integer, b::OrdinalSuffixedInteger)
return (a * th):b
end

function Base.:(<)(a::OrdinalSuffixedInteger, b::OrdinalSuffixedInteger)
return (cardinal(a) < cardinal(b))
end
Base.:(<)(a::OrdinalSuffixedInteger, b::Integer) = (a < b * th)
Base.:(<)(a::Integer, b::OrdinalSuffixedInteger) = (a * th < b)
function Base.:(<=)(a::OrdinalSuffixedInteger, b::OrdinalSuffixedInteger)
return (cardinal(a) <= cardinal(b))
end
Base.:(<=)(a::OrdinalSuffixedInteger, b::Integer) = (a <= b * th)
Base.:(<=)(a::Integer, b::OrdinalSuffixedInteger) = (a * th <= b)

function Broadcast.broadcasted(
::Broadcast.DefaultArrayStyle{1},
::typeof(*),
r::UnitRange,
t::OrdinalSuffixedInteger{One},
)
return (first(r) * t):(last(r) * t)
end
function Broadcast.broadcasted(
::Broadcast.DefaultArrayStyle{1},
::typeof(*),
r::Base.OneTo,
t::OrdinalSuffixedInteger{One},
)
return Base.OneTo(last(r) * t)
end

function Base.show(io::IO, ordinal::OrdinalSuffixedInteger)
n = cardinal(ordinal)
m = n % 10
if m == 1
return print(io, n, n == 11 ? "th" : "st")
elseif m == 2
return print(io, n, n == 12 ? "th" : "nd")
elseif m == 3
return print(io, n, n == 13 ? "th" : "rd")
end
return print(io, n, "th")
end
include("one.jl")
include("ordinalsuffixedinteger.jl")
end
12 changes: 12 additions & 0 deletions src/lib/OrdinalIndexing/src/one.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
struct One <: Integer end
const 𝟏 = One()
Base.convert(type::Type{<:Number}, ::One) = one(type)
Base.promote_rule(type1::Type{One}, type2::Type{<:Number}) = type2
Base.:(*)(x::One, y::One) = 𝟏

# Needed for Julia 1.7.
Base.convert(::Type{One}, ::One) = One()

function Base.show(io::IO, ordinal::One)
return print(io, "𝟏")
end
108 changes: 108 additions & 0 deletions src/lib/OrdinalIndexing/src/ordinalsuffixedinteger.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
struct OrdinalSuffixedInteger{T<:Integer} <: Integer
cardinal::T
function OrdinalSuffixedInteger{T}(cardinal::Integer) where {T<:Integer}
cardinal 0 || throw(ArgumentError("ordinal must be > 0"))
return new{T}(cardinal)
end
end
function OrdinalSuffixedInteger(cardinal::Integer)
return OrdinalSuffixedInteger{typeof(cardinal)}(cardinal)
end
function OrdinalSuffixedInteger{T}(ordinal::OrdinalSuffixedInteger) where {T<:Integer}
return OrdinalSuffixedInteger{T}(cardinal(ordinal))
end

cardinal(ordinal::OrdinalSuffixedInteger) = getfield(ordinal, :cardinal)
function cardinal_type(ordinal_type::Type{<:OrdinalSuffixedInteger})
return fieldtype(ordinal_type, :cardinal)
end

const th = OrdinalSuffixedInteger(𝟏)
const st = th
const nd = th
const rd = th

function Base.widen(ordinal_type::Type{<:OrdinalSuffixedInteger})
return OrdinalSuffixedInteger{widen(cardinal_type(ordinal_type))}
end

Base.Int(ordinal::OrdinalSuffixedInteger) = Int(cardinal(ordinal))

function Base.:(*)(a::OrdinalSuffixedInteger, b::Integer)
return OrdinalSuffixedInteger(cardinal(a) * b)
end
function Base.:(*)(a::Integer, b::OrdinalSuffixedInteger)
return OrdinalSuffixedInteger(a * cardinal(b))
end
function Base.:(:)(
start::OrdinalSuffixedInteger{T}, stop::OrdinalSuffixedInteger{T}
) where {T<:Integer}
return UnitRange{OrdinalSuffixedInteger{T}}(start, stop)
end

function Base.:(*)(a::OrdinalSuffixedInteger, b::OrdinalSuffixedInteger)
return (cardinal(a) * cardinal(b)) * th
end
function Base.:(+)(a::OrdinalSuffixedInteger, b::OrdinalSuffixedInteger)
return (cardinal(a) + cardinal(b)) * th
end
function Base.:(+)(a::OrdinalSuffixedInteger, b::Integer)
return a + b * th
end
function Base.:(+)(a::Integer, b::OrdinalSuffixedInteger)
return a * th + b
end
function Base.:(-)(a::OrdinalSuffixedInteger, b::OrdinalSuffixedInteger)
return (cardinal(a) - cardinal(b)) * th
end
function Base.:(-)(a::OrdinalSuffixedInteger, b::Integer)
return a - b * th
end
function Base.:(-)(a::Integer, b::OrdinalSuffixedInteger)
return a * th - b
end

function Base.:(:)(a::Integer, b::OrdinalSuffixedInteger)
return (a * th):b
end

function Base.:(<)(a::OrdinalSuffixedInteger, b::OrdinalSuffixedInteger)
return (cardinal(a) < cardinal(b))
end
Base.:(<)(a::OrdinalSuffixedInteger, b::Integer) = (a < b * th)
Base.:(<)(a::Integer, b::OrdinalSuffixedInteger) = (a * th < b)
function Base.:(<=)(a::OrdinalSuffixedInteger, b::OrdinalSuffixedInteger)
return (cardinal(a) <= cardinal(b))
end
Base.:(<=)(a::OrdinalSuffixedInteger, b::Integer) = (a <= b * th)
Base.:(<=)(a::Integer, b::OrdinalSuffixedInteger) = (a * th <= b)

function Broadcast.broadcasted(
::Broadcast.DefaultArrayStyle{1},
::typeof(*),
r::UnitRange,
t::OrdinalSuffixedInteger{One},
)
return (first(r) * t):(last(r) * t)
end
function Broadcast.broadcasted(
::Broadcast.DefaultArrayStyle{1},
::typeof(*),
r::Base.OneTo,
t::OrdinalSuffixedInteger{One},
)
return Base.OneTo(last(r) * t)
end

function Base.show(io::IO, ordinal::OrdinalSuffixedInteger)
n = cardinal(ordinal)
m = n % 10
if m == 1
return print(io, n, n == 11 ? "th" : "st")
elseif m == 2
return print(io, n, n == 12 ? "th" : "nd")
elseif m == 3
return print(io, n, n == 13 ? "th" : "rd")
end
return print(io, n, "th")
end

0 comments on commit 5cab9cf

Please sign in to comment.