Skip to content

Commit

Permalink
Add copy(xf, [T,] foldable)
Browse files Browse the repository at this point in the history
  • Loading branch information
tkf committed Nov 5, 2019
1 parent f382360 commit 53fa63b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Transducers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ 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

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
Expand Down
19 changes: 19 additions & 0 deletions src/processes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 53fa63b

Please sign in to comment.