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 fb6973d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46"

[compat]
ArgCheck = "1"
BangBang = "0.2, 0.3"
BangBang = "0.3.2"
InitialValues = "0.2"
Requires = "0.5"
Setfield = "0.3, 0.4, 0.5"
Expand Down
1 change: 1 addition & 0 deletions docs/src/manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ dtransduce
eduction
map!
copy!
copy
append!
Transducers.append!!
collect
Expand Down
2 changes: 1 addition & 1 deletion src/Transducers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions src/processes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,26 @@ 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 there is no consistent interface to create an empty
container given its type and not all containers support creating an empty
container.)
"""
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 fb6973d

Please sign in to comment.