Skip to content

Commit

Permalink
Replace rf.xform with xform(rf)
Browse files Browse the repository at this point in the history
  • Loading branch information
tkf committed Jan 16, 2019
1 parent 7d5778a commit a86ad26
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 70 deletions.
6 changes: 3 additions & 3 deletions examples/transducers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# ones:

using Transducers
using Transducers: Transducer, R_, next, inner
using Transducers: Transducer, R_, next, inner, xform

# ## Stateless transducer

Expand Down Expand Up @@ -68,7 +68,7 @@ nothing # hide

function Transducers.start(rf::R_{RandomRecall}, result)
buffer = InType(rf)[]
rng = MersenneTwister(rf.xform.seed)
rng = MersenneTwister(xform(rf).seed)
private_state = (buffer, rng)
return wrap(rf, private_state, start(inner(rf), result))
end
Expand All @@ -82,7 +82,7 @@ function Transducers.next(rf::R_{RandomRecall}, result, input)
wrapping(rf, result) do (buffer, rng), iresult
# Pickup a random element to be passed to the inner reducing function.
# Replace it with the new incoming one in the buffer:
if length(buffer) < rf.xform.history
if length(buffer) < xform(rf).history
push!(buffer, input)
iinput = rand(rng, buffer)
else
Expand Down
19 changes: 10 additions & 9 deletions src/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ InType(T::Type) = throw(MethodError(InType, (T,)))
Setfield.constructor_of(::Type{T}) where {T <: AbstractReduction} = T

inner(rf::AbstractReduction) = rf.inner
xform(rf::AbstractReduction) = rf.xform

# In clojure a reduction function is one of signature
# whatever, input -> whatever
Expand All @@ -102,15 +103,15 @@ struct Reduction{intype, X <: Transducer, I} <: AbstractReduction{intype}
end

Transducer(rf::Reduction{<:Any, <:Transducer, <:AbstractReduction}) =
Composition(rf.xform, Transducer(inner(rf)))
Transducer(rf::Reduction) = rf.xform
Composition(xform(rf), Transducer(inner(rf)))
Transducer(rf::Reduction) = xform(rf)

"""
Transducers.R_{X}
When defining a transducer type `X`, it is often required to dispatch
on type `rf::R_{X}` (Reducing Function) which bundles the current
transducer `rf.xform::X` and the inner reducing function
transducer `xform(rf)::X` and the inner reducing function
`inner(rf)::R_`.
"""
const R_{X} = Reduction{<:Any, <:X}
Expand Down Expand Up @@ -214,12 +215,12 @@ combine(rf::Reduction, a, b) =
# Not using dispatch to avoid ambiguity
if a isa PrivateState{typeof(rf)}
# TODO: make sure this branch is compiled out
error("Stateful transducer ", rf.xform, " does not support `combine`")
error("Stateful transducer ", xform(rf), " does not support `combine`")
elseif b isa PrivateState{typeof(rf)}
error("""
Some thing went wrong in two ways:
* `combine(rf, a, b)` is called but type of `a` and `b` are different.
* `rf.xform = $(rf.xform)` is stateful and does not support `combine`.
* `xform(rf) = $(xform(rf))` is stateful and does not support `combine`.
""")
else
combine(inner(rf), a, b)
Expand Down Expand Up @@ -346,20 +347,20 @@ finaltype(rf::Reduction) =
if inner(rf) isa AbstractReduction
finaltype(inner(rf))
else
outtype(rf.xform, InType(rf))
outtype(xform(rf), InType(rf))
end

# isexpansive(::Any) = true
isexpansive(::Transducer) = true
isexpansive(::AbstractFilter) = false
# isexpansive(rf::Reduction) = isexpansive(rf.xform) || isexpansive(inner(rf))
# isexpansive(rf::Reduction) = isexpansive(xform(rf)) || isexpansive(inner(rf))
isexpansive(xf::Composition) = isexpansive(xf.outer) || isexpansive(xf.inner)
# Should it be a type-level trait?

#=
iscontractive(::Any) = false
iscontractive(::AbstractFilter) = true
iscontractive(rf::Reduction) = iscontractive(rf.xform) && iscontractive(inner(rf))
iscontractive(rf::Reduction) = iscontractive(xform(rf)) && iscontractive(inner(rf))
=#

struct NoComplete <: Transducer end
Expand Down Expand Up @@ -523,7 +524,7 @@ end
initvalue(x, ::Any) = x
initvalue(init::Initializer, intype) = init.f(intype)

_initvalue(rf::Reduction) = initvalue(rf.xform.init, InType(rf))
_initvalue(rf::Reduction) = initvalue(xform(rf).init, InType(rf))

inittypeof(::T, ::Type) where T = T
function inittypeof(init::Initializer, intype::Type)
Expand Down
Loading

0 comments on commit a86ad26

Please sign in to comment.