Skip to content

Commit

Permalink
Add an example with transducers (#3)
Browse files Browse the repository at this point in the history
* Add an example with transducers

* Fix doctest
  • Loading branch information
tkf authored Jun 29, 2020
1 parent 6018146 commit e434dfd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/LazyGroupBy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ function _import_docs()
try
include_dependency(path)
str = read(path, String)
str = replace(str, r"^```julia"m => "```jldoctest")
str = replace(str, r"^```julia"m => "```jldoctest $name")
@eval @doc $str $name
catch err
@error(
Expand Down
30 changes: 30 additions & 0 deletions src/docs/reduce.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,33 @@ Transducers.GroupByViewDict{Bool,Mean{Float64,EqualWeight},…} with 2 entries:
false => Mean: n=3 | value=1.33333
true => Mean: n=7 | value=4.71429
```

An example for calculating the minimum, maximum, and number of each
group in one go:

```julia
julia> using Transducers

julia> table = ((k = gcd(v, 42), v = v) for v in 1:100);

julia> collect(Iterators.take(table, 5)) # preview
5-element Array{NamedTuple{(:k, :v),Tuple{Int64,Int64}},1}:
(k = 1, v = 1)
(k = 2, v = 2)
(k = 3, v = 3)
(k = 2, v = 4)
(k = 1, v = 5)

julia> counter = reducingfunction(Map(_ -> 1), +);

julia> reduce.(TeeRF(min, max, counter), Map(x -> x.v), grouped(x -> x.k, table))
Transducers.GroupByViewDict{Int64,Tuple{Int64,Int64,Int64},} with 8 entries:
7 => (7, 91, 5)
14 => (14, 98, 5)
42 => (42, 84, 2)
2 => (2, 100, 29)
3 => (3, 99, 15)
21 => (21, 63, 2)
6 => (6, 96, 14)
1 => (1, 97, 28)
```

0 comments on commit e434dfd

Please sign in to comment.