Giter VIP home page Giter VIP logo

itertools's Introduction

Itertools for golang

Build Status

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 i
  • Cycle(iter) - Infinite cycling of iter (requires memory)
  • Repeat(element [, n]) - Repeat element n times (or infinitely)

Finite Iterator Creators

  • New(elements ...) - Create from interface{} elements
  • Int32(elements ...) - Create from int32 elements
  • Int64(elements ...) - Create from int64 elements
  • Uint(elements ...) - Create from uint elements
  • Uint32(elements ...) - Create from uint32 elements
  • Uint64(elements ...) - Create from uint64 elements
  • Float32(elements ...) - Create from float32 elements
  • Float64(elements ...) - Create from float64 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 iterators
  • DropWhile(predicate, iter) - Drop elements until predicate(el) == false
  • TakeWhile(predicate, iter) - Take elements until predicate(el) == false
  • Filter(predicate, iter) - Filter out elements when predicate(el) == false
  • FilterFalse(predicate, iter) - Filter out elements when predicate(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 to mapper(el)
  • MultiMap(multiMapper func(interface{}...)interface{}, iters...) - Map all the iterators as variadic arguments to multiMaper(elements...). Stop on shortest iterator.
  • MultiMapLongest(multiMapper func(interface{}...)interface{}, iters...) - Same as MultiMap except stop on longest iterator. Shorter iterators are filled with nil after they are exhausted.
  • Starmap(multiMapper func(interface{}...)interface{}, iter) - If iter is an iterator of []interface{}, then expand it into the multiMapper.
  • Zip(iters...) - Zip multiple iterators together
  • ZipLongest(iters...) - Zip multiple iterators together. Take the longest. Shorter ones are appended with nil.
  • 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.

itertools's People

Contributors

yanatan16 avatar damienklinnert avatar lucretiel avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.