diff --git a/src/Transducers.jl b/src/Transducers.jl index f5d24e0b6b..28627bceb7 100644 --- a/src/Transducers.jl +++ b/src/Transducers.jl @@ -6,7 +6,7 @@ export Transducer, Map, Filter, Cat, MapCat, Take, PartitionBy, Scan, Zip, TakeLast, FlagFirst, MapSplat, ScanEmit, Enumerate, NotA, OfType, transduce, eduction, setinput, Reduced, reduced, unreduced, ifunreduced, Completing, OnInit, CopyInit, right, reducingfunction, dreduce, dtransduce, - withprogress, AdHocFoldable + withprogress, AdHocFoldable, into # Deprecated: export Distinct @@ -14,7 +14,7 @@ export Distinct using Base.Broadcast: Broadcasted using ArgCheck -using BangBang: BangBang, append!!, empty!!, push!!, setindex!! +using BangBang: BangBang, Empty, append!!, empty!!, push!!, setindex!! using Distributed: Distributed, @everywhere using Logging: LogLevel, @logmsg using Requires diff --git a/src/processes.jl b/src/processes.jl index 1a112992f6..d4ae9a0c41 100644 --- a/src/processes.jl +++ b/src/processes.jl @@ -590,6 +590,25 @@ function Base.collect(xf::Transducer, coll) end # Base.collect(xf, coll) = append!([], xf, coll) +""" + copy(xf::Transducer, T, foldable) :: Union{T, Nothing} + copy(xf::Transducer, foldable::T) :: Union{T, Nothing} + +Process `foldable` with a transducer `xf` and then create a container of type `T` +filled with the result. Return `nothing` if the transducer does not produce +anything. This is because not all containers support creating an empty +container in a type-stable manner. +""" +function Base.copy(xf::Transducer, ::Type{T}, foldable) where T + result = append!!(Empty(T), foldable) + if result isa Empty + return nothing + end + return result +end + +Base.copy(xf::Transducer, foldable::T) where T = copy(xf, T, foldable) + """ map!(xf::Transducer, dest, src; simd)