From fb6973def07359d1879117ec26a2577f85cc7273 Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Mon, 4 Nov 2019 01:12:49 -0800 Subject: [PATCH] Add `copy(xf, [T,] foldable)` --- Project.toml | 2 +- docs/src/manual.md | 1 + src/Transducers.jl | 2 +- src/processes.jl | 20 ++++++++++++++++++++ 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index b7ff1dc7e6..ee166b39cb 100644 --- a/Project.toml +++ b/Project.toml @@ -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" diff --git a/docs/src/manual.md b/docs/src/manual.md index 6a992795df..f62ad26531 100644 --- a/docs/src/manual.md +++ b/docs/src/manual.md @@ -19,6 +19,7 @@ dtransduce eduction map! copy! +copy append! Transducers.append!! collect diff --git a/src/Transducers.jl b/src/Transducers.jl index f5d24e0b6b..d5e6af172f 100644 --- a/src/Transducers.jl +++ b/src/Transducers.jl @@ -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..7f57cb2c46 100644 --- a/src/processes.jl +++ b/src/processes.jl @@ -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)