• Stars
    star
    296
  • Rank 140,464 (Top 3 %)
  • Language
    Go
  • Created over 11 years ago
  • Updated almost 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A golang implementation of the python standard library module.

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.

More Repositories

1

nanoajax

An ajax library you need a microscope to see
JavaScript
247
star
2

golang-instagram

A minimalist golang instagram API wrapper
Go
74
star
3

golang-soundcloud

A SoundCloud API implementation in Go
Go
42
star
4

clj-kubernetes-api

Kubernetes API Client library in Clojure
Clojure
40
star
5

fyra

Experimental framework based on Functional Relational (not Reactive!) Programming
Clojure
30
star
6

kafka-connect-slack-sink

Kafka Connector Sink to post to slack via webhooks
Clojure
19
star
7

goauth2

OAuth 2.0 Implementation
Go
12
star
8

golang-spsa

Simultaneous Perturbation Stochastic Approximation method in Go
Go
11
star
9

gulp-packer

Minify JS using /packer/ with gulp
JavaScript
10
star
10

golang-mtgox

A Mt. Gox Websocket API Implementation in Go
Go
7
star
11

pebble-bold-hour

A pebble watchface with a bold hour and a small minute.
C
7
star
12

d3-tsp-demo

A Traveling Salesman Problem (TSP) solver using d3.js (demo)
7
star
13

kubernetes-linode-setup

Setting up a kubernetes cluster on linode
Shell
7
star
14

multipart-pipe

Pipe multipart uploads direct to S3 or another file service
JavaScript
5
star
15

franzy-connect

Clojurized Kafka Connect Interface
Clojure
5
star
16

tethr.io

A socket.io-like API for Peer to Peer WebRTC Data Streams
JavaScript
5
star
17

node-api-routes

A declarative system for creating express API routes.
JavaScript
4
star
18

kafka-connect-s3-sink

Kafka Connector that writes records to S3 as files
Clojure
4
star
19

haskell-kafka

An Apache Kafka driver in Haskell
Haskell
4
star
20

node-redis-eval

A small package to manage redis EVAL SCRIPT caching and EVALSHA calling
JavaScript
3
star
21

sicp-club

Book club to read SICP
Scheme
3
star
22

yanatan16.github.io

My website based on the Jekyll framework
SCSS
3
star
23

docker-autobuilder

Docker Automated builds support for private repositories
Lua
3
star
24

tybee

An asset trading system written in Haskell
Haskell
3
star
25

denvr

Development Environments Reimagined
Clojure
3
star
26

haskell-spsa

Simultaneous Perturbation Stochastic Approximation in Haskell
Haskell
3
star
27

pycodejam

A Code Jam problem runner for python
JavaScript
3
star
28

kafka-connect-pg-sink

Kafka Connector for pushing records as tuples in PostgreSQL
Clojure
3
star
29

cljs-node-app-template

Lein template for a clojurescript node app
Clojure
3
star
30

revealme

A cloud presentation format using reveal.js
CSS
3
star
31

node-fanin

A simple concurrency pattern: fan-in
JavaScript
3
star
32

talk-clojurescript-for-javascript-devs

Talk: Clojurescript for Javascript Developers
Clojure
3
star
33

RoboNUI

Robotic Natural User Interface using Kinect and COTS Robot Kits in C#
C#
3
star
34

mongodb-indexing-presentation

Content for a presentation on MongoDB Indexing.
Shell
2
star
35

gochat

A chat server in Go
Go
2
star
36

talk-frelp-ui

Talk: Functional Relational Programming in UI Development
HTML
1
star
37

minerva

Some old research on Predictive Optimal Investing
Python
1
star
38

hnefatafl

tafl for everyone
Clojure
1
star
39

mgoconf

Small configuration file reading for mgo
Go
1
star
40

haskell-pipes-wc

`wc` in haskell using pipes
Haskell
1
star
41

exchequer

An arbitrary object query utility belt for Go
Go
1
star
42

stripe-ctf3-level4

Solution to Stripe CTF3 Level 4: Distributed SQL Database
Python
1
star
43

pebble-binary

A pebble watchface which displays Month, Day, Hour, Minute, and Second in binary.
C
1
star
44

mongoose-pusher

Mongoose plugin to allow a "pusher" function which acts like setter for arrays.
JavaScript
1
star
45

node-marked-sections

A simple node package to parse markdown and insert sections around headings.
JavaScript
1
star
46

haskell-chp-wc

Implementation of `wc` in Haskell using CHP
Haskell
1
star
47

joneisen.works

Business Website
JavaScript
1
star
48

ProtoStat

Prototype Bench Processor Statusing, Scheduling, and Logging WebApp in php
PHP
1
star
49

react-simple-example

Simple react/redux with semantic ui example
JavaScript
1
star
50

twitter-graph-analysis

A few scripts to help analyze a twitter graph
Python
1
star
51

zuul-demo

A demo for node's zuul test runner
JavaScript
1
star
52

node-unix-listen

Simple but comprehensive unix socket listening for connect servers
JavaScript
1
star