diff --git a/base/Base.jl b/base/Base.jl index 777e07bd30715..0a8669a1e9450 100644 --- a/base/Base.jl +++ b/base/Base.jl @@ -83,11 +83,19 @@ include("traits.jl") include("range.jl") include("error.jl") +# Some type +include("some.jl") + # core numeric operations & types include("bool.jl") include("number.jl") include("int.jl") include("operators.jl") + +# The following definitions are not in `operators.jl` so they are not seen by bootstrapping (`compiler.jl`) +getproperty(f::Fix1, s::Symbol) = s === :x ? getx(f) : getfield(f, s) +getproperty(f::Fix2, s::Symbol) = s === :x ? getx(f) : getfield(f, s) + include("pointer.jl") include("refvalue.jl") include("refpointer.jl") @@ -149,8 +157,6 @@ end include("multimedia.jl") using .Multimedia -# Some type -include("some.jl") include("dict.jl") include("abstractset.jl") diff --git a/base/compiler/compiler.jl b/base/compiler/compiler.jl index 873ce764bccbf..17cd1850c0fc8 100644 --- a/base/compiler/compiler.jl +++ b/base/compiler/compiler.jl @@ -40,6 +40,9 @@ include("range.jl") include("expr.jl") include("error.jl") +# Some type +include("some.jl") + # core numeric operations & types include("bool.jl") include("number.jl") diff --git a/base/operators.jl b/base/operators.jl index 605c0775ad00f..8cd1df2dc9860 100644 --- a/base/operators.jl +++ b/base/operators.jl @@ -913,6 +913,7 @@ julia> filter(!isletter, str) """ !(f::Function) = (x...)->!f(x...) +#= """ Fix1(f, x) @@ -946,6 +947,40 @@ struct Fix2{F,T} <: Function end (f::Fix2)(y) = f.f(y, f.x) +=# + +interleave(bind::Tuple{}, args::Tuple{}) = () +interleave(bind::Tuple{}, args::Tuple) = error("more args than positions") +interleave(bind, args) = _interleave(first(bind), tail(bind), args) + +# `nothing` indicates a position to be bound +_interleave(firstbind::Nothing, tailbind::Tuple, args::Tuple) = ( + first(args), interleave(tailbind, tail(args))...) + +# allow escaping of e.g. `nothing` +_interleave(firstbind::Some{T}, tailbind::Tuple, args::Tuple) where T = ( + something(firstbind), interleave(tailbind, args)...) + +_interleave(firstbind::T, tailbind::Tuple, args::Tuple) where T = ( + firstbind, interleave(tailbind, args)...) + +struct Bind{F, A} <: Function + f::F + a::A +end + +function (c::Bind)(args...) + c.f(interleave(c.a, args)...) +end + +const Fix1{F, X} = Bind{F, Tuple{Some{X}, Nothing}} +const Fix2{F, X} = Bind{F, Tuple{Nothing, Some{X}}} +Fix1(f, x) = Bind(f, (Some(x), nothing)) +Fix2(f, x) = Bind(f, (nothing, Some(x))) + +getx(f::Fix1) = something(f.a[1]) +getx(f::Fix2) = something(f.a[2]) + """ isequal(x) diff --git a/base/strings/search.jl b/base/strings/search.jl index 9d2fdf73afff4..9dea295a9d0bf 100644 --- a/base/strings/search.jl +++ b/base/strings/search.jl @@ -9,7 +9,7 @@ function findnext(pred::Fix2{<:Union{typeof(isequal),typeof(==)},<:AbstractChar} throw(BoundsError(s, i)) end @inbounds isvalid(s, i) || string_index_err(s, i) - c = pred.x + c = getx(pred) c ≤ '\x7f' && return nothing_sentinel(_search(s, c % UInt8, i)) while true i = _search(s, first_utf8_byte(c), i) @@ -20,10 +20,10 @@ function findnext(pred::Fix2{<:Union{typeof(isequal),typeof(==)},<:AbstractChar} end findfirst(pred::Fix2{<:Union{typeof(isequal),typeof(==)},<:Union{Int8,UInt8}}, a::ByteArray) = - nothing_sentinel(_search(a, pred.x)) + nothing_sentinel(_search(a, getx(pred))) findnext(pred::Fix2{<:Union{typeof(isequal),typeof(==)},<:Union{Int8,UInt8}}, a::ByteArray, i::Integer) = - nothing_sentinel(_search(a, pred.x, i)) + nothing_sentinel(_search(a, getx(pred), i)) function _search(a::Union{String,ByteArray}, b::Union{Int8,UInt8}, i::Integer = 1) if i < 1 @@ -48,7 +48,7 @@ end function findprev(pred::Fix2{<:Union{typeof(isequal),typeof(==)},<:AbstractChar}, s::String, i::Integer) - c = pred.x + c = getx(pred) c ≤ '\x7f' && return nothing_sentinel(_rsearch(s, c % UInt8, i)) b = first_utf8_byte(c) while true @@ -60,10 +60,10 @@ function findprev(pred::Fix2{<:Union{typeof(isequal),typeof(==)},<:AbstractChar} end findlast(pred::Fix2{<:Union{typeof(isequal),typeof(==)},<:Union{Int8,UInt8}}, a::ByteArray) = - nothing_sentinel(_rsearch(a, pred.x)) + nothing_sentinel(_rsearch(a, getx(pred))) findprev(pred::Fix2{<:Union{typeof(isequal),typeof(==)},<:Union{Int8,UInt8}}, a::ByteArray, i::Integer) = - nothing_sentinel(_rsearch(a, pred.x, i)) + nothing_sentinel(_rsearch(a, getx(pred), i)) function _rsearch(a::Union{String,ByteArray}, b::Union{Int8,UInt8}, i::Integer = sizeof(a)) if i < 1