Skip to content
This repository has been archived by the owner on Sep 1, 2020. It is now read-only.

Commit

Permalink
Make order of arguments for groupby consistent with other iterators. …
Browse files Browse the repository at this point in the history
…Fix groupby performance test.
  • Loading branch information
lendle committed Feb 2, 2015
1 parent a4a741c commit 6267fa7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,13 @@ Install this package with `Pkg.add("Iterators")`
i = (7,8,9)
```

- **groupby**(xs, f)
- **groupby**(f, xs)

Group consecutive values that share the same result of applying `f`.

Example:
```julia
for i in groupby(["face", "foo", "bar", "book", "baz", "zzz"], x -> x[1])
for i in groupby(x -> x[1], ["face", "foo", "bar", "book", "baz", "zzz"])
@show i
end
```
Expand Down
11 changes: 8 additions & 3 deletions src/Iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -435,15 +435,20 @@ done(it::Partition, state) = done(it.xs, state[1])
# ["bar", "book", "baz"]
# ["zzz"]
immutable GroupBy{I}
xs::I
keyfunc::Function
xs::I
end

eltype{I}(it::GroupBy{I}) = I
eltype{I<:Ranges}(it::GroupBy{I}) = Array{eltype(it.xs),}

function groupby(xs, keyfunc)
GroupBy(xs, keyfunc)
function groupby(xs, keyfunc::Function)
Base.warn_once("groupby(xs, keyfunc) should be groupby(keyfunc, xs)")
groupby(keyfunc, xs)
end

function groupby(keyfunc, xs)
GroupBy(keyfunc, xs)
end

function start(it::GroupBy)
Expand Down
10 changes: 5 additions & 5 deletions test/perf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,16 @@ abstract GroupBy
type GroupBy1 <: GroupBy end
type GroupBy2 <: GroupBy end

collect(::GroupBy, v, f) = Base.collect(groupby(v, f))
collect(::GroupBy, f, v) = Base.collect(groupby(f, v))

function Base.run{Itr<:GroupBy}(p::Collect{Itr}, n::Int, state)
v, f = state
f, v = state
op = Itr()
collect(op, v, f)
collect(op, f, v)
end

Base.start(::Collect{GroupBy1}, n::Int) = (["abc"[[rand(1:3), rand(1:3)]] for i = 1:n], x->x[1])
Base.start(::Collect{GroupBy2}, n::Int) = (1:n, x->div(x,2))
Base.start(::Collect{GroupBy1}, n::Int) = (x->x[1], ["abc"[[rand(1:3), rand(1:3)]] for i = 1:n])
Base.start(::Collect{GroupBy2}, n::Int) = (iseven, [1:n])

############

Expand Down

0 comments on commit 6267fa7

Please sign in to comment.