Exts.jl
Usage
pkg> registry add https://github.com/0h7z/0hjl.git
pkg> add Exts
julia> using Exts
julia> Exts.ext(:) # to see which extensions are loaded
8-element Vector{Pair{Symbol, Union{Nothing, Module}}}:
:Base => Exts.BaseExt
:DataFrames => DataFramesExt
:Dates => DatesExt
:FITSIO => FITSIOExt
:FITSIODataFrames => FITSIODataFramesExt
:Pkg => PkgExt
:Statistics => StatisticsExt
:YAML => YAMLExt
API reference
Types
Exts.Datum
— TypeDatum{T} <- Union{T, Missing}
datum(T::Type...) -> Datum{Union{T...}}
Exts.IntOrStr
— TypeIntOrStr <- Union{AbstractString, Integer}
Exts.Maybe
— TypeMaybe{T} <- Union{T, Nothing}
maybe(T::Type...) -> Maybe{Union{T...}}
Exts.SymOrStr
— TypeSymOrStr <- Union{AbstractString, Symbol}
Exts.VType
— TypeVType <- Union{Type, TypeofVararg}
Exts.VecOrTup
— TypeVecOrTup <- Union{AbstractVector, Tuple}
Exts.LDict
— TypeLDict <- LittleDict
An ordered dictionary type for small numbers of keys. Rather than using hash
or some other sophisticated measure to store the vals in a clever arrangement, it just keeps everything in a pair of lists.
While theoretically this has expected time complexity $O(n)$ (vs the hash-based OrderedDict
/Dict
's expected time complexity $O(1)$, and the search-tree-based SortedDict
's expected time complexity $O(\log n)$), in practice it is really fast, because it is cache & SIMD friendly.
It is reasonable to expect it to outperform an OrderedDict
, with up to around 30 elements in general; or with up to around 50 elements if using a LittleDict
backed by Tuple
s (see freeze
). However, this depends on exactly how long isequal
and hash
take, as well as on how many hash collisions occur etc.
When constructing a LittleDict
it is faster to pass in the keys and values each as separate lists. So if you have them seperately already, do LittleDict(ks, vs)
not LittleDict(zip(ks, vs))
. Furthermore, key and value lists that are passed as Tuple
s will not require any copies to create the LittleDict
, so LittleDict(ks::Tuple, vs::Tuple)
is the fastest constructor of all.
When constructing a LittleDict
, unlike hash-based OrderedDict
/Dict
, it does not guarantee that the keys are unique but assumes so. Use at your own risk if there might be duplicate keys.
See also LittleDicts.
Exts.ODict
— TypeODict <- OrderedDict
OrderedDict
s are simply dictionaries whose entries have a particular order. The order refers to insertion order, which allows deterministic iteration over the dictionary.
See also OrderedDicts.
Exts.OSet
— TypeOSet <- OrderedSet
OrderedSet
s are simply sets whose entries have a particular order. The order refers to insertion order, which allows deterministic iteration over the set.
See also OrderedSets.
Exts.UDict
— TypeUDict <- Dict # UnorderedDict
See Base.Dict
.
Exts.USet
— TypeUSet <- Set # UnorderedSet
See Base.Set
.
Exts.VTuple
— TypeVTuple{T} <- VTuple0{T}
VTuple0{T} <- Tuple{Vararg{T}} # 0 or more
VTuple1{T} <- Tuple{T, Vararg{T}} # 1 or more
VTuple2{T} <- Tuple{T, T, Vararg{T}} # 2 or more
VTuple3{T} <- Tuple{T, T, T, Vararg{T}} # 3 or more
Macros
Exts.@S_str
— Macro@S_str -> Symbol
Create a Symbol
from a literal string.
Examples
julia> S"Julia"
:Julia
julia> S"$0 expands to the name of the shell or shell script."
Symbol("\$0 expands to the name of the shell or shell script.")
Exts.@catch
— Macro@catch expr default = nothing
Evaluate the expression with a try/catch
construct and return the thrown exception. If no error (exception) occurs, return default
.
See also The try/catch
statement, try/catch
, @trycatch
, @try
.
Exts.@disp
— Macro@disp expr[...] -> Nothing
Evaluate an expression and display the result.
Examples
julia> @disp Maybe{AbstractVecOrMat}
Union{Nothing, AbstractVecOrMat}
julia> @disp Maybe{AbstractVecOrMat}...
Nothing
AbstractVector (alias for AbstractArray{T, 1} where T)
AbstractMatrix (alias for AbstractArray{T, 2} where T)
julia> @disp "+-×÷"...
'+': ASCII/Unicode U+002B (category Sm: Symbol, math)
'-': ASCII/Unicode U+002D (category Pd: Punctuation, dash)
'×': Unicode U+00D7 (category Sm: Symbol, math)
'÷': Unicode U+00F7 (category Sm: Symbol, math)
Exts.@noinfo
— Macro@noinfo expr
Suppress all lexically-enclosed uses of @debug
, @info
.
Exts.@nowarn
— Macro@nowarn expr
Suppress all lexically-enclosed uses of @debug
, @info
, @warn
.
Exts.@try
— Macro@try expr default = nothing
Evaluate the expression with a try/catch
construct and return the result. If any error (exception) occurs, return default
.
See also The try/catch
statement, try/catch
, @trycatch
, @catch
.
Exts.@trycatch
— Macro@trycatch expr func = identity
Evaluate the expression with a try/catch
construct and return either the result or the thrown exception (passed to and processed by func
), whichever available.
See also The try/catch
statement, try/catch
, @try
, @catch
.
Functions
Exts.cis
— MethodExts.cis(x::Real) -> Complex{<:AbstractFloat}
More accurate method for exp(im*x)
(especially for large x).
See also ∠
, polar
, Base.cis
, cispi
.
Examples
julia> -1 == -Exts.cis(2π) == Exts.cis(-π) == Exts.cis(π)
true
julia> -1 != -Base.cis(2π) != Base.cis(-π) != Base.cis(π)
true
Exts.datum
— FunctionDatum{T} <- Union{T, Missing}
datum(T::Type...) -> Datum{Union{T...}}
Exts.ext
— MethodExts.ext(::Colon) -> Vector{Pair{Symbol, Maybe{Module}}}
Exts.ext
— MethodExts.ext(x::Symbol) -> Maybe{Module}
Exts.floatminmax
— Functionfloatminmax(T = Float64)
Return (floatmin(T), floatmax(T))
.
See floatmin
, floatmax
. See also maxintfloat
.
Exts.freeze
— Methodfreeze(d::AbstractDict{K, V}) where {K, V} -> LDict{<:K, <:V}
Render a dictionary immutable by converting it to a Tuple
-backed LittleDict
. The Tuple
-backed LittleDict
is faster than the Vector
-backed LittleDict
, particularly when the keys are all concretely typed.
Exts.invsqrt
— Methodinvsqrt(x::T) where T <: Real -> AbstractFloat
Return $\sqrt{x^{-1}}$.
See also sqrt
.
Examples
julia> invsqrt(4)
0.5
Exts.isdir
— MethodExts.isdir(path::AbstractString) -> Bool
Return true
if path
is a directory, false
otherwise.
Examples
julia> Exts.isdir(homedir())
true
julia> Exts.isdir("not/a/directory")
false
Exts.isdirpath
— MethodExts.isdirpath(path::AbstractString) -> Bool
Determine whether a path refers to a directory (for example, ends with a path separator).
Examples
julia> Exts.isdirpath("/home")
false
julia> Exts.isdirpath("/home/")
true
Exts.lmap
— Functionlmap(f, iterators...)
Create a lazy mapping. This is another syntax for writing (f(args...) for args in zip(iterators...))
. Equivalent to Iterators.map
.
See also map
.
Examples
julia> collect(lmap(x -> x^2, 1:3))
3-element Vector{Int64}:
1
4
9
Exts.maybe
— FunctionMaybe{T} <- Union{T, Nothing}
maybe(T::Type...) -> Maybe{Union{T...}}
Exts.notmissing
— MethodExts.polar
— Methodpolar(radius::Real, azimuth::Real) -> Complex{<:AbstractFloat}
Return $r∠θ$, where $θ$ is in degrees. Equivalent to radius∠(azimuth)
.
See also ∠
.
Exts.readstr
— Methodreadstr(x) -> String
Read the entirety of x
as a string. Equivalent to read(x, String)
.
Exts.stdpath
— Methodstdpath(path::AbstractString...; real = false) -> String
Standardize a path (or a set of paths, by joining them together), removing "." and ".." entries and changing path separator to the standard "/".
If path
is a directory, the returned path will end with a "/".
If real
is true, symbolic links are expanded, however the path
must exist in the filesystem. On case-insensitive case-preserving filesystems, the filesystem's stored case for the path is returned.
Examples
julia> stdpath("/home/user/../example.jl")
"/home/example.jl"
julia> stdpath("Documents\\Julia\\")
"Documents/Julia/"
Exts.typeminmax
— MethodExts.walkdir
— FunctionExts.walkdir(path::AbstractString = pwd(); topdown = true) -> Channel
Return an iterator that walks the directory tree of a directory.
The iterator returns a tuple containing (path, dirs, files)
. Each iteration path
will change to the next directory in the tree; then dirs
and files
will be vectors containing the directories and files in the current path
directory. The directory tree can be traversed top-down or bottom-up. The returned iterator is stateful so when accessed repeatedly each access will resume where the last left off, like Iterators.Stateful
.
See also readdir
, Base.walkdir
.
Examples
for (path, ds, fs) ∈ Exts.walkdir(".")
@info "Directories in $path"
for d ∈ ds
println(path * d) # path to directories
end
@info "Files in $path"
for f ∈ fs
println(path * f) # path to files
end
end
Exts.∠
— Method∠(azimuth::Real) -> Complex{<:AbstractFloat}
Return $1∠θ$, where $θ$ is in degrees. Equivalent to polar(1, azimuth)
.