Giter Site home page Giter Site logo

go-explore's Introduction

go-explore

In this repo I experiment with Golang.

While the packages within may be in a usable state, they are still highly experimental and subject to change.

Itertools

Porting Python's itertools module (and a bit more) into Go.

It is built around the Iterator[T] interface:

type Iterator[T any] interface {
	// Next tries to advance to the next value.
	// Returns true if a value exists, false if not.
	// Once an iterator returns false to indicate exhaustion,
	// it should continue returning false.
	Next() bool
	// Value returns the current value of the iterator.
	// Next() must be called and return true before every call to Value().
	Value() T
}

Iteration is fairly straightforward:

for iter := Literal(1,2,3); iter.Next(); {
	fmt.Println(iter.Value())
}

Would result in

1
2
3

Fibonacci

A simple Fibonacci iterator can be written as follows (itertools. remove for brevity):

func Fibonacci() Iterator[int] {
	a, b := 1, 1
	advance := func() (bool, int) {
		retval := a
		a, b = b, a + b
		return true, retval
	}
	return FromAdvance(advance)
}

Note that this iterator is "infinite". It does not stop after a set number, but keeps going. If we only want to print the first 10 elements, we can Take the first 10 elements and print them:

ForEach(
    Take(                           // Take the first
        10,                         // 10 elements
        Fibonacci(),                // from our Fibonacci sequence
    ),
    func(v int) { fmt.Println(v) }, // and print them
)

Resulting in

1
1 
2 
3 
5 
8 
13
21
34
55

go-explore's People

Contributors

tmr232 avatar

Stargazers

 avatar  avatar

Watchers

 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.