Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Heptazhou committed May 9, 2024
1 parent 39dad1f commit 04f7d89
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Exts"
uuid = "0b12d779-4123-4875-9d6c-e33c2e29e2c9"
authors = ["Heptazhou <zhou at 0h7z dot com>"]
version = "0.1.0"
version = "0.1.1"

[deps]
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
Expand Down
7 changes: 7 additions & 0 deletions src/BaseExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ function Base.adjoint(m::T)::AbstractMatrix{<:AbstractString} where T <: Abstrac
permutedims(m)
end

function Base.convert(::Type{S}, v::AbstractVector) where S <: AbstractSet
S(eltype(S)[v;])
end
function Base.convert(::Type{S}, v::AbstractVector) where S <: AbstractSet{T} where T
S(T[v;])
end

function Base.log10(x::T, σ::T) where T <: Real
# https://physics.stackexchange.com/q/95254
log10(x), σ / log(10)x
Expand Down
11 changes: 11 additions & 0 deletions src/Exts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@

module Exts

export getfirst
export getlast
export invsqrt
export nanmean
export readstr

using Reexport: @reexport

Expand All @@ -25,6 +28,14 @@ using Reexport: @reexport

include("BaseExt.jl")

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)

readstr(x)::String = read(x, String)

function invsqrt(x::T) where T <: Real
F::Type = float(T)
F(big(x) |> inv |> sqrt)
Expand Down
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@
using Test

@testset "BaseExt" begin
@test_throws MethodError convert(Set, 1:3)
@test_throws MethodError convert(Set{Int}, 1:3)
@test_throws MethodError log10(11, 2)
@test_throws MethodError repr([:a, 1]')
@test_throws UndefVarError invsqrt(1.0)
@test_throws UndefVarError readstr(@__FILE__)
using Exts

@test [:_ -1] == [:_, -1]'
@test [:p :q] == [:p, :q]'
@test ['1' '2'] == ['1', '2']'
@test ["x" "y"] == ["x", "y"]'
@test chomp(readstr(@__FILE__)) == readchomp(@__FILE__)
@test convert(Set{Int}, 1:3) == convert(Set, 1:3) == Set(1:3)
@test invsqrt(2^-2) == 2
@test_nowarn log10(11, 2)
end
Expand Down

0 comments on commit 04f7d89

Please sign in to comment.