Skip to content

Commit

Permalink
Merge b2a7790 into 0fdd763
Browse files Browse the repository at this point in the history
  • Loading branch information
tkf committed Jan 14, 2019
2 parents 0fdd763 + b2a7790 commit bdf4d0a
Show file tree
Hide file tree
Showing 15 changed files with 533 additions and 53 deletions.
4 changes: 4 additions & 0 deletions Project.toml
Expand Up @@ -6,6 +6,10 @@ version = "0.1.0"
[deps]
ArgCheck = "dce04be8-c92d-5529-be00-80e4d2c0e197"
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46"

[compat]
Setfield = "0.3"

[extras]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
2 changes: 2 additions & 0 deletions docs/Project.toml
@@ -1,7 +1,9 @@
[deps]
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46"

[compat]
Documenter = "0.21"
Literate = "1"
Setfield = "0.3.2"
1 change: 1 addition & 0 deletions docs/src/doctests/index.md
@@ -0,0 +1 @@
# Doctests
147 changes: 147 additions & 0 deletions docs/src/doctests/show_rf.md
@@ -0,0 +1,147 @@
# Show method for reducing functions

```@meta
DocTestSetup = quote
using Transducers
using Transducers: Reduction, TeeZip
using Setfield: @set!
end
```

```jldoctest
Reduction(
Filter(isfinite) |> Map(sin),
+,
Float64)
# output
Reduction{▶ Float64}(
Filter(isfinite),
Reduction{▶ Float64}(
Map(sin),
+))
```

```jldoctest
rf = Reduction(
TeeZip(Count()),
right,
Float64)
@set! rf.inner.inner.value = 1.25
# output
Splitter{▶ Float64}(
Reduction{▶ Float64}(
Count(1, 1),
Joiner{▶ ⦃Float64, Int64⦄}(
Transducers.right,
1.25)),
(@lens _.inner.value))
```

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

```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
# output
Splitter{▶ Float64}(
Reduction{▶ Float64}(
Count(1, 1),
Splitter{▶ Int64}(
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))
```

```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
# output
Splitter{▶ Int64}(
Reduction{▶ Int64}(
Filter(isodd),
Reduction{▶ Int64}(
Map(identity),
Splitter{▶ Int64}(
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))
```

```jldoctest
rf = Reduction(
TeeZip(Filter(isodd) |> Map(identity) |> TeeZip(Map(identity))),
right,
Any)
# output
Splitter{▶ Any}(
Reduction{▶ Any}(
Filter(isodd),
Reduction{▶ Any}(
Map(identity),
Splitter{▶ Any}(
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))
```

```@meta
DocTestSetup = nothing
```
3 changes: 3 additions & 0 deletions docs/utils.jl
Expand Up @@ -41,6 +41,9 @@ transducers_makedocs(;
"Manual" => "manual.md",
"Interface" => "interface.md",
hide("Internals" => "internals.md"),
hide("Doctests" => "doctests/index.md", [
"doctests/show_rf.md",
]),
"Examples" => examples,
],
repo = "https://github.com/tkf/Transducers.jl/blob/{commit}{path}#L{line}",
Expand Down
3 changes: 3 additions & 0 deletions src/Transducers.jl
Expand Up @@ -9,6 +9,9 @@ export Map, Filter, Cat, MapCat, Take, PartitionBy, Scan, Zip,

using ArgCheck

import Setfield
using Setfield: @lens, set

include("basics.jl")
include("core.jl")
include("library.jl")
Expand Down
13 changes: 10 additions & 3 deletions src/core.jl
Expand Up @@ -59,6 +59,9 @@ end

struct IdentityTransducer <: Transducer end

has(xf::Transducer, T::Type) = xf isa T
has(xf::Composition, T::Type) = has(xf.outer, T) || has(xf.inner, T)

"""
Transducer
Expand All @@ -74,6 +77,8 @@ appropriately defined for child types.
"""
AbstractFilter

abstract type AbstractReduction end

# In clojure a reduction function is one of signature
# whatever, input -> whatever
#
Expand All @@ -83,16 +88,18 @@ AbstractFilter
# a transducer `xform` and an inner reduction function `inner`.
# `inner` can be either a `Reduction` or a function with arity-2 and arity-1 methods
#
struct Reduction{X <: Transducer, I, InType}
struct Reduction{X <: Transducer, I, InType} <: AbstractReduction
xform::X
inner::I
end

Setfield.constructor_of(::Type{T}) where {T <: Reduction} = T

InType(::T) where T = InType(T)
InType(::Type{Reduction{X, I, intype}}) where {X, I, intype} = intype
InType(T::Type) = throw(MethodError(InType, (T,)))

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

Expand Down Expand Up @@ -337,7 +344,7 @@ Output item type for the transducer `xf` when the input type is `intype`.
outtype(::Any, ::Any) = Any
outtype(::AbstractFilter, intype) = intype

finaltype(rf::Reduction{<:Transducer, <:Reduction}) = finaltype(rf.inner)
finaltype(rf::Reduction{<:Transducer, <:AbstractReduction}) = finaltype(rf.inner)
finaltype(rf::Reduction) = outtype(rf.xform, InType(rf))

# isexpansive(::Any) = true
Expand Down

0 comments on commit bdf4d0a

Please sign in to comment.