Itertools for golang
This package is a translation of the python itertools
module. It includes all the usual suspects except for the cartesian product and permutation operators. All iterators are chan interface{}
which allows some type ambiguity for these generic functions. It would be completely ok, however, to reproduce these functions in your package for your type-specific iterators such as chan MyStruct
. I did this mostly as a thought exercise on converting python generators to Go.
Full documentation is available on godoc.
Implemented Functions
Infinite Iterator Creators
Count(i)
- Infinite count from iCycle(iter)
- Infinite cycling ofiter
(requires memory)Repeat(element [, n])
- Repeat element n times (or infinitely)
Finite Iterator Creators
New(elements ...)
- Create frominterface{}
elementsInt32(elements ...)
- Create fromint32
elementsInt64(elements ...)
- Create fromint64
elementsUint(elements ...)
- Create fromuint
elementsUint32(elements ...)
- Create fromuint32
elementsUint64(elements ...)
- Create fromuint64
elementsFloat32(elements ...)
- Create fromfloat32
elementsFloat64(elements ...)
- Create fromfloat64
elements
Iterator Destroyers
Reduce(iter, reducer, memo)
- Reduce (or Foldl) across the iterator.List(iter)
- Create a list from the iterator
Iterator Modifiers
Chain(iters...)
- Chain together multiple iteratorsDropWhile(predicate, iter)
- Drop elements untilpredicate(el) == false
TakeWhile(predicate, iter)
- Take elements untilpredicate(el) == false
Filter(predicate, iter)
- Filter out elements whenpredicate(el) == false
FilterFalse(predicate, iter)
- Filter out elements whenpredicate(el) == true
Slice(iter, start[, stop[, step]])
- Drop elements until the start (0-based index). Stop upon stop (exclusive) unless not given. Step is 1 unless given.
More Iterator Modifiers
Map(mapper func(interface{}) interface{}, iter)
- Map each element tomapper(el)
MultiMap(multiMapper func(interface{}...)interface{}, iters...)
- Map all the iterators as variadic arguments tomultiMaper(elements...)
. Stop on shortest iterator.MultiMapLongest(multiMapper func(interface{}...)interface{}, iters...)
- Same as MultiMap except stop on longest iterator. Shorter iterators are filled withnil
after they are exhausted.Starmap(multiMapper func(interface{}...)interface{}, iter)
- If iter is an iterator of[]interface{}
, then expand it into themultiMapper
.Zip(iters...)
- Zip multiple iterators togetherZipLongest(iters...)
- Zip multiple iterators together. Take the longest. Shorter ones are appended withnil
.Tee(iter, n)
- Split an iterator into n equal versions.Tee2(iter)
- Split an iterator into two equal versions
Benchmarks
BenchmarkFilter 200000 41188 ns/op
BenchmarkNoFilter 5000000 640 ns/op
BenchmarkMap 10000 108577 ns/op
BenchmarkNoMap 10000000 321 ns/op
BenchmarkReduce 1000000 1962 ns/op
BenchmarkNoReduce 100000000 22.0 ns/op
Clearly this package is much slower than doing just the original operations. However, all large systems inevitably involve abstractions and layers of indirection which are convinient while adding overhead. A system designer should think carefully before choosing one over the other. To be clear, this package was written as a thought experiment, and while usable, might represent a pattern to be rewritten using your types in your project to save the cost of typecasting overhead.
Thanks to damienklinnert for the benchmarks.
License
Copyright (c) 2013 Jon Eisen
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.