Skip to content

Commit

Permalink
Merge 85dfd43 into a79a15e
Browse files Browse the repository at this point in the history
  • Loading branch information
tkf committed Jan 16, 2019
2 parents a79a15e + 85dfd43 commit 2f2e6bf
Show file tree
Hide file tree
Showing 9 changed files with 321 additions and 394 deletions.
86 changes: 43 additions & 43 deletions docs/src/doctests/show_rf.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,93 +28,93 @@ rf = Reduction(
TeeZip(Count()),
right,
Float64)
@set! rf.inner.inner.value = 1.25
@set! rf.xforms[3].xform.value = 1.25
# output
Splitter{▶ Float64}(
Reduction{▶ Float64}(
Splitter((@lens _.xforms[2].xform.value)),
Reduction{▶ Float64}(
Count(1, 1),
Joiner{▶ ⦃Float64, Int64⦄}(
Transducers.right,
1.25)),
(@lens _.inner.value))
Reduction{▶ Int64}(
Joiner(1.25),
Transducers.right)))
```

```jldoctest
rf = Reduction(
Filter(isfinite) |> TeeZip(Count()) |> Enumerate() |> MapSplat(*),
right,
Float64)
@set! rf.inner.inner.inner.value = 1.25
@set! rf.xforms[4].xform.value = 1.25
# output
Reduction{▶ Float64}(
Filter(isfinite),
Splitter{▶ Float64}(
Reduction{▶ Float64}(
Splitter((@lens _.xforms[2].xform.value)),
Reduction{▶ Float64}(
Count(1, 1),
Joiner{▶ ⦃Float64, Int64⦄}(
Reduction{▶ Int64}(
Joiner(1.25),
Reduction{▶ ⦃Float64, Int64⦄}(
Enumerate(1, 1),
Reduction{▶ ⦃Int64, ⦃Float64, Int64⦄⦄}(
MapSplat(*),
Transducers.right)),
1.25)),
(@lens _.inner.value)))
Transducers.right))))))
```

```jldoctest
rf = Reduction(
TeeZip(Count() |> TeeZip(FlagFirst())),
right,
Float64)
@set! rf.inner.inner.inner.inner.value = 123456789
@set! rf.inner.inner.inner.inner.inner.value = 1.25
@set! rf.xforms[5].xform.value = 123456789
@set! rf.xforms[6].xform.value = 1.25
# output
Splitter{▶ Float64}(
Reduction{▶ Float64}(
Splitter((@lens _.xforms[5].xform.value)),
Reduction{▶ Float64}(
Count(1, 1),
Splitter{▶ Int64}(
Reduction{▶ Int64}(
Splitter((@lens _.xforms[2].xform.value)),
Reduction{▶ Int64}(
FlagFirst(),
Joiner{▶ ⦃Int64, ⦃Bool, Int64⦄⦄}(
Joiner{▶ ⦃Float64, ⦃Int64, ⦃Bool, Int64⦄⦄⦄}(
Transducers.right,
1.25),
123456789)),
(@lens _.inner.value))),
(@lens _.inner.inner.inner.inner.value))
Reduction{▶ ⦃Bool, Int64⦄}(
Joiner(123456789),
Reduction{▶ ⦃Int64, ⦃Bool, Int64⦄⦄}(
Joiner(1.25),
Transducers.right))))))
```

```jldoctest
rf = Reduction(
TeeZip(Filter(isodd) |> Map(identity) |> TeeZip(Map(identity))),
right,
Int64)
@set! rf.inner.inner.inner.inner.inner.value = 123456789
@set! rf.inner.inner.inner.inner.inner.inner.value= 123456789
@set! rf.xforms[6].xform.value = 123456789
@set! rf.xforms[7].xform.value = 123456789
# output
Splitter{▶ Int64}(
Reduction{▶ Int64}(
Splitter((@lens _.xforms[6].xform.value)),
Reduction{▶ Int64}(
Filter(isodd),
Reduction{▶ Int64}(
Map(identity),
Splitter{▶ Int64}(
Reduction{▶ Int64}(
Splitter((@lens _.xforms[2].xform.value)),
Reduction{▶ Int64}(
Map(identity),
Joiner{▶ ⦃Int64, Int64⦄}(
Joiner{▶ ⦃Int64, ⦃Int64, Int64⦄⦄}(
Transducers.right,
123456789),
123456789)),
(@lens _.inner.value)))),
(@lens _.inner.inner.inner.inner.inner.value))
Reduction{▶ Int64}(
Joiner(123456789),
Reduction{▶ ⦃Int64, Int64⦄}(
Joiner(123456789),
Transducers.right)))))))
```

```jldoctest
Expand All @@ -125,21 +125,21 @@ rf = Reduction(
# output
Splitter{▶ Any}(
Reduction{▶ Any}(
Splitter((@lens _.xforms[6].xform.value)),
Reduction{▶ Any}(
Filter(isodd),
Reduction{▶ Any}(
Map(identity),
Splitter{▶ Any}(
Reduction{▶ Any}(
Splitter((@lens _.xforms[2].xform.value)),
Reduction{▶ Any}(
Map(identity),
Joiner{▶ ⦃Any, Any⦄}(
Joiner{▶ ⦃Any, ⦃Any, Any⦄⦄}(
Transducers.right,
nothing),
nothing)),
(@lens _.inner.value)))),
(@lens _.inner.inner.inner.inner.inner.value))
Reduction{▶ Any}(
Joiner(nothing),
Reduction{▶ ⦃Any, Any⦄}(
Joiner(nothing),
Transducers.right)))))))
```

```@meta
Expand Down
16 changes: 8 additions & 8 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
using Transducers: Transducer, R_, next, inner, xform

# ## Stateless transducer

Expand All @@ -17,7 +17,7 @@ struct AddOneIfInt <: Transducer end
function Transducers.next(rf::R_{AddOneIfInt}, result, input)
if input isa Int
# Output `input + 1` is passed to the "inner" reducing step:
next(rf.inner, result, input + 1)
next(inner(rf), result, input + 1)
else
# Filtering out is done by "doing nothing"; return `result`-so-far
# as-is:
Expand Down Expand Up @@ -68,9 +68,9 @@ 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(rf.inner, result))
return wrap(rf, private_state, start(inner(rf), result))
end

# Stateful transducer needs to unwrap its private state inside
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 All @@ -92,7 +92,7 @@ function Transducers.next(rf::R_{RandomRecall}, result, input)
end
# Call the inner reducing function. Note that `iresult` unwrapped by
# [`Transducers.wrapping`](@ref) must be passed to `next`:
iresult = next(rf.inner, iresult, iinput)
iresult = next(inner(rf), iresult, iinput)
return (buffer, rng), iresult
end
end
Expand All @@ -118,10 +118,10 @@ function Transducers.complete(rf::R_{RandomRecall}, result)
for x in buffer
# Note that inner `next` can be called more than one time inside
# `next` and `complete`:
iresult = next(rf.inner, iresult, x)
iresult = next(inner(rf), iresult, x)
end
# `complete` for inner reducing function must be called exactly once:
return complete(rf.inner, iresult)
return complete(inner(rf), iresult)
end

# This then adds 3 (`= RandomRecall().history`) more elements to the
Expand Down
40 changes: 40 additions & 0 deletions examples/tutorial_missings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,43 @@ mapfoldl(xf_argmax(), right, [1, 3, missing, 2])
# the index of the largest odd value:

mapfoldl(xf_argmax(Filter(isodd)), right, [1, 4, 3, 2])

# ## Extrema

argext_step(should_update) =
((argext, ext), (index, value)) ->
should_update(ext, value) ? (index, value) : (argext, ext)

argext_init(::typeof(>), ::Type{Tuple{F, S}}) where {F, S} = (zero(F), typemax(S))
argext_init(::typeof(<), ::Type{Tuple{F, S}}) where {F, S} = (zero(F), typemin(S))

xf_scanext(should_update) =
Scan(argext_step(should_update),
Initializer(TT -> argext_init(should_update, TT)))
nothing # hide

ans = # hide
mapfoldl(Zip(Count(), NotA(Missing)) |> xf_scanext(<), right, [1.0, 3.0, missing, 2.0])
#-
@assert ans === (2, 3.0) # hide
@show ans

xf_fullextrema(xf_filter = NotA(Missing)) =
Zip(Count(), xf_filter) |> Zip(xf_scanext(>), xf_scanext(<))

ans = # hide
mapfoldl(xf_fullextrema(), right, [1.0, 3.0, -1.0, 2.0])
#-
@assert ans === ((3, -1.0), (2, 3.0)) # hide
@show ans

xf_argextrema(xf_filter = NotA(Missing)) =
xf_fullextrema(xf_filter) |> Map() do ((argmin, min), (argmax, max))
(argmin, argmax)
end

ans = # hide
mapfoldl(xf_argextrema(), right, [1.0, 3.0, -1.0, 2.0])
#-
@assert ans === (3, 2) # hide
@show ans

0 comments on commit 2f2e6bf

Please sign in to comment.