Skip to content
Merged
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
7 changes: 7 additions & 0 deletions src/ThreadsX.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ else
@eval const $(Symbol("@spawn")) = $(Symbol("@async"))
end

if isdefined(Transducers, :foldxt)
using Transducers: foldxt
else
foldxt(rf, xf, xs; kw...) = reduce(rf, xf, xs; kw...)
foldxt(rf, xs; kw...) = reduce(rf, Map(identity), xs; kw...)
end

include("utils.jl")
include("basesizes.jl")
include("reduce.jl")
Expand Down
4 changes: 2 additions & 2 deletions src/foreach.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ ThreadsX.foreach(f, array::AbstractArray, arrays::AbstractArray; kw...) =

@inline return_nothing(_...) = nothing

ThreadsX.foreach(f::F, itr; kw...) where {F} = reduce(
ThreadsX.foreach(f::F, itr; kw...) where {F} = foldxt(
return_nothing,
Map(f),
itr;
Expand All @@ -124,7 +124,7 @@ ThreadsX.foreach(f::F, itr; kw...) where {F} = reduce(
kw...,
)

ThreadsX.foreach(f::F, itr, itrs...; kw...) where {F} = reduce(
ThreadsX.foreach(f::F, itr, itrs...; kw...) where {F} = foldxt(
return_nothing,
MapSplat(f),
zip(itr, itrs...);
Expand Down
18 changes: 9 additions & 9 deletions src/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ without_basesize(; basesize = nothing, kw...) = kw

function ThreadsX.reduce(op, itr; kw...)
xf, reducible = extract_transducer(itr)
result = reduce(
result = foldxt(
op,
xf,
reducible;
Expand All @@ -16,7 +16,7 @@ end

function ThreadsX.mapreduce(f, op, itr; kw...)
xf, reducible = extract_transducer(itr)
result = reduce(
result = foldxt(
op,
opcompose(xf, Map(f)),
reducible;
Expand All @@ -33,7 +33,7 @@ function ThreadsX.mapreduce(f, op, itr, itrs...; kw...)
# `Base` just does `reduce(op, map(f, ...))`:
return mapreduce(f, op, itr, itrs...; without_basesize(; kw...)...)
end
return reduce(
return foldxt(
op,
MapSplat(f),
zip(itr, itrs...);
Expand Down Expand Up @@ -64,7 +64,7 @@ asbool(f) = x -> f(x)::Bool

# TODO: `any` and `all` should be done with "unordered" version
ThreadsX.any(itr; kw...) = ThreadsX.any(identity, itr; kw...)
ThreadsX.any(f, itr; kw...) = reduce(
ThreadsX.any(f, itr; kw...) = foldxt(
right, # no need to use `|`
opcompose(Map(asbool(f)), ReduceIf(identity)),
simd = Val(true),
Expand All @@ -75,7 +75,7 @@ ThreadsX.any(f, itr; kw...) = reduce(
)

ThreadsX.all(itr; kw...) = ThreadsX.all(identity, itr; kw...)
ThreadsX.all(f, itr; kw...) = reduce(
ThreadsX.all(f, itr; kw...) = foldxt(
right, # no need to use `&`
opcompose(Map(asbool(f)), ReduceIf(!)),
itr;
Expand All @@ -86,7 +86,7 @@ ThreadsX.all(f, itr; kw...) = reduce(
)

ThreadsX.findfirst(itr; kw...) = ThreadsX.findfirst(identity, itr; kw...)
ThreadsX.findfirst(f, array::AbstractArray; kw...) = reduce(
ThreadsX.findfirst(f, array::AbstractArray; kw...) = foldxt(
right,
ReduceIf(i -> f(@inbounds array[i])),
keys(array);
Expand All @@ -99,7 +99,7 @@ ThreadsX.findfirst(f, array::AbstractArray; kw...) = reduce(
ThreadsX.findlast(itr; kw...) = ThreadsX.findlast(identity, itr; kw...)
function ThreadsX.findlast(f, array::AbstractArray; kw...)
idx = keys(array)
return reduce(
return foldxt(
right,
opcompose(Map(i -> (@inbounds idx[i])), ReduceIf(i -> f(@inbounds array[i]))),
lastindex(idx):-1:firstindex(idx);
Expand All @@ -125,7 +125,7 @@ end
_minmax((min0, max0), (min1, max1)) = (min(min0, min1), max(max0, max1))

ThreadsX.extrema(itr; kw...) = ThreadsX.extrema(identity, itr; kw...)
ThreadsX.extrema(f, itr; kw...) = reduce(
ThreadsX.extrema(f, itr; kw...) = foldxt(
asmonoid(_minmax),
Map(x -> (y = f(x); (y, y))),
itr;
Expand Down Expand Up @@ -189,7 +189,7 @@ function ThreadsX.unique(f::F, itr::AbstractVector{X}; kw...) where {F,X}
# Using inference as an optimization. The result of this inference
# does not affect the result:
Y = Core.Compiler.return_type(f, Tuple{X})
ys, = reduce(
ys, = foldxt(
PushUnique(f),
Map(identity),
itr;
Expand Down