Skip to content

WIP Generalize Fix1 and Fix2 #36180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions base/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -149,8 +157,6 @@ end
include("multimedia.jl")
using .Multimedia

# Some type
include("some.jl")

include("dict.jl")
include("abstractset.jl")
Expand Down
3 changes: 3 additions & 0 deletions base/compiler/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
35 changes: 35 additions & 0 deletions base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,7 @@ julia> filter(!isletter, str)
"""
!(f::Function) = (x...)->!f(x...)

#=
"""
Fix1(f, x)

Expand Down Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions base/strings/search.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down