Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Heptazhou committed Nov 6, 2024
1 parent 007a0da commit d51e43e
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 46 deletions.
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
name = "Exts"
uuid = "0b12d779-4123-4875-9d6c-e33c2e29e2c9"
authors = ["Heptazhou <zhou at 0h7z dot com>"]
version = "0.2.0"
version = "0.2.1"

[deps]
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"

[weakdeps]
Expand All @@ -23,4 +24,5 @@ StatisticsExt = "StatsBase"
[compat]
CFITSIO = "≥ 1.4.2"
FITSIO = "≥ 0.17.2"
OrderedCollections = "≥ 1.5"
julia = "≥ 1.9"
7 changes: 4 additions & 3 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
using Documenter: Documenter, DocMeta
using DocumenterInterLinks: InterLinks
using Exts
using OrderedCollections: OrderedDict
using HTTP: HTTP

DocMeta.setdocmeta!(Exts, :DocTestSetup, quote
#! format: noindent
Expand All @@ -26,10 +26,11 @@ using DataFrames: DataFrames
using FITSIO: FITSIO
using StatsBase: StatsBase

const entry = OrderedDict{String, String}()
const cache = URL::String -> (URL, HTTP.download(URL * "objects.inv", mktempdir()))
const entry = ODict{String, String}()
const extra = Vector{Module}()
const links = InterLinks(
"Julia" => "https://docs.julialang.org/en/v1/",
"Julia" => cache("https://docs.julialang.org/en/v1/"),
)

cd(@__DIR__) do
Expand Down
29 changes: 28 additions & 1 deletion src/Exts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ module Exts
export datum
export Datum
export IntOrStr
export LDict
export maybe
export Maybe
export ODict
export OSet
export SymOrStr
export UDict
export USet
export VecOrTup
export VTuple

Expand All @@ -28,14 +33,19 @@ export @noinfo
export @nowarn
export @try
export @trycatch
export
export dropmissing
export dropnothing
export ensure_vector
export flatten
export getfirst
export getlast
export invsqrt
export lmap
export nanmean
export notmissing
export pause
export polar
export readstr
export return_type
export stdpath
Expand All @@ -45,8 +55,9 @@ using Reexport: @reexport

@reexport begin
#! format: noindent
using Base: nonmissingtype, nonnothingtype, return_types
using Base: nonnothingtype, notnothing, return_types
using Base.Threads: @spawn, @threads, nthreads
using OrderedCollections: LittleDict, OrderedDict, OrderedSet, freeze
end

include("Macro.jl")
Expand All @@ -63,15 +74,31 @@ end
ensure_vector(a::AbstractArray) = eachslice(a, dims = ndims(a))
ensure_vector(v::AbstractVector) = v

flatten(itr) = collect(Iterators.flatten(itr))

getfirst(predicate::Function, A) = A[findfirst(predicate, A)]
getfirst(predicate::Function) = Base.Fix1(getfirst, predicate)

getlast(predicate::Function, A) = A[findlast(predicate, A)]
getlast(predicate::Function) = Base.Fix1(getlast, predicate)

return_type(xs...; kw...) = only(return_types(xs...; kw...)::Vector)

slash(x::AbstractString)::String = replace(x, '\\' => '/')

function pause(msg::Maybe{AbstractString} = nothing; ante::Int = 0, post::Int = 0)::Nothing
print(stdout, '\n'^ante)
pause(stdin, stdout, msg)
print(stdout, '\n'^post)
end
function pause(in::IO, out::IO, msg::Maybe{AbstractString} = nothing)::Nothing
print(out, @something msg """Press any key to continue . . . """)
ccall(:jl_tty_set_mode, Int32, (Ptr{Cvoid}, Int32), in.handle, 1)
read(in, Char)
ccall(:jl_tty_set_mode, Int32, (Ptr{Cvoid}, Int32), in.handle, 0)
print(out, '\n')
end

include("BaseExt.jl")
include("Function.jl")

Expand Down
108 changes: 79 additions & 29 deletions src/Function.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,25 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

"""
Exts.cis(x::Real) -> Complex{<:AbstractFloat}
More accurate method for `exp(im*x)` (especially for large x).
See also [`∠`](@ref), [`polar`](@ref), [`Base.cis`](@extref),
[`cispi`](@extref Base.cispi).
# Examples
```jldoctest
julia> -1 == -Exts.cis(2π) == Exts.cis(-π) == Exts.cis(π)
true
julia> -1 != -Base.cis(2π) != Base.cis(-π) != Base.cis(π)
true
```
"""
cis(theta::Real)::Complex{<:AbstractFloat} = cispi(theta / π)

"""
Exts.ext(::Colon) -> VTuple{Pair{Symbol, Maybe{Module}}}
"""
Expand All @@ -32,35 +51,6 @@ function ext(x::Symbol)::Maybe{Module}
Base.get_extension(Exts, Symbol(x, :Ext))
end

@doc """
lmap(f, iterators...)
Create a lazy mapping. This is another syntax for writing `(f(args...) for
args in zip(iterators...))`. Equivalent to [`Iterators.map`](@extref
Base.Iterators.map).
See also [`map`](@extref Base.map).
# Examples
```jldoctest
julia> collect(lmap(x -> x^2, 1:3))
3-element Vector{Int64}:
1
4
9
```
"""
lmap = Iterators.map

@doc raw"""
readstr(x) -> String
Read the entirety of `x` as a string. Equivalent to `read(x, String)`.
See also [`read`](@extref Base.read), [`readchomp`](@extref Base.readchomp).
"""
readstr(x)::String = read(x, String)

"""
invsqrt(x::T) -> AbstractFloat where T <: Real
Expand Down Expand Up @@ -124,6 +114,66 @@ function isdirpath(path::AbstractString)::Bool
Base.isdirpath(path)
end

@doc """
lmap(f, iterators...)
Create a lazy mapping. This is another syntax for writing `(f(args...) for
args in zip(iterators...))`. Equivalent to [`Iterators.map`](@extref
Base.Iterators.map).
See also [`map`](@extref Base.map).
# Examples
```jldoctest
julia> collect(lmap(x -> x^2, 1:3))
3-element Vector{Int64}:
1
4
9
```
"""
lmap = Iterators.map

"""
notmissing(x)
Throw an error if `x === missing`, and return `x` if not.
See also [`notnothing`](@extref Base.notnothing).
"""
notmissing(x::Any) = x
notmissing(::Missing) = throw(ArgumentError("missing passed to notmissing"))

"""
polar(radius::Real, azimuth::Real) -> Complex{<:AbstractFloat}
Return ``r∠θ``, where ``θ`` is in degrees. Equivalent to `radius∠(azimuth)`.
See also [`∠`](@ref).
"""
function polar(radius::Real, azimuth::Real)::Complex{<:AbstractFloat}
cispi(azimuth / 180) * radius
end
"""
∠(azimuth::Real) -> Complex{<:AbstractFloat}
Return ``1∠θ``, where ``θ`` is in degrees. Equivalent to `polar(1, azimuth)`.
See also [`polar`](@ref), [`Exts.cis`](@ref).
"""
function (azimuth::Real)::Complex{<:AbstractFloat}
cispi(azimuth / 180) # 360° = 2π rad
end

@doc raw"""
readstr(x) -> String
Read the entirety of `x` as a string. Equivalent to `read(x, String)`.
See also [`read`](@extref Base.read), [`readchomp`](@extref Base.readchomp).
"""
readstr(x)::String = read(x, String)

"""
stdpath(path::AbstractString...; real = false) -> String
Expand Down
12 changes: 12 additions & 0 deletions src/Type.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,15 @@ const maybe(T::Type...) = Maybe{Union{T...}}
maybe(T::Type...) -> Maybe{Union{T...}}
""" Maybe, maybe

const LDict = LittleDict
const ODict = OrderedDict
const OSet = OrderedSet
const UDict = Dict
const USet = Set

@doc " LDict -> LittleDict" LDict
@doc " ODict -> OrderedDict" ODict
@doc " OSet -> OrderedSet" OSet
@doc " UDict -> Dict # UnorderedDict" UDict
@doc " USet -> Set # UnorderedSet" USet

Loading

0 comments on commit d51e43e

Please sign in to comment.