-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
122 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |