Giter Club home page Giter Club logo

go-itergen's Introduction

go-itergen Build Status

go-itergen addresses a common and big problem of go: no maps, no filters, no nothing. If you come from a functional background that could be really frustrating. Since generics are not going to be around for a while code generation is the only way we have to achieve such things.

This is a naive attempt to make easier deal with this kind of operations over iterable types without having to write every single time the same code over and over.

It also has a very nice feature: it works with channels, not just slices. So, you can map, filter, concat and reduce channels. And even convert them to arrays. All without always having to write the same boilerplate code. TL;DR: channel manipulation made easy.

Install

go get -u github.com/erizocosmico/go-itergen/cmd/...

Available operations

go-itergen generates the following functions for a slice type:

  • Map: apply a function to every element and return a slice/channel with the modifications. It actually returns a XXXIterMapResult, which will have a set of operations to convert the interface{} result to other types.
  • Filter: apply a function and will return a slice/channel with all the elements whose result was true.
  • All (only for slices): will return true if all the elements return true after applying the given function.
  • Some (only for slices): will return true if any of the elements return true after applying the given function.
  • Concat:
    • In the case of a slice, will return a new array with the contents of a and b given a.Concat(b).
    • In the case of a channel, will return a new channel multiplexing all the other given channels. E.g: a.Concat(b, c, d) will return a channel with all the items sent to a, b, c and d.
  • Find (only for slices): will return the item and the index of the first occurrence that returns true after applying the given function.
  • ForEach: will execute a function for every item in the slice/channel.
  • Reverse (only for slices): will return the slice in reversed order.
  • Splice (only for slices): will return a new slice with a number of items removed after the given start.
  • Reduce: applies a function against an accumulator and each value of the slice/channel (from first to last) to reduce it to a single value of the given type.
  • Array (only for channels): converts the channel into an array. The operation blocks, but can be done in a goroutine and you will be notified via the done parameter.

You can choose which operations you want for your type, that is, if you don't need Map or another function it won't be generated.

Generate code

You just have to add that to a file in the package you want the code to be generated in.

//go:generate go-itergen -t "float64" --pkg="mypkg" --map="string" --map="int" --filter --all --some --foreach --concat --find --reverse --splice --reduce="string" --reduce="int"

Or execute the binary:

go-itergen -t "float64" --pkg="mypkg" --map="string" --map="int" --filter --all --some --foreach --concat --find --reverse --splice --reduce="string" --reduce="int"

Types from external packages

If you want to generate an iterable type for an external package, you can do that with :. For example, let's say we want a os.File iterable. We would do it like this:

go-itergen -t "os:os.File" --pkg="mypkg" --map="string"

Note that what goes before the : is what would go in the import, and that you have to type the full type name. This is because of how Go packages work. One can not guarantee that github.com/foo/go-bar will be go-bar.Foo. It may be bar.Foo. And thus, it has to be specified.

Another example:

go-itergen -t "golang.org/x/net/context:context.Context" --pkg="mypkg" --map="github.com/foo/ctx:ctx.MyCtx"

Take a look at the map. We can also specify the external packages in map and reduce arguments.

Example

For examples of generated code see the examples folder. Contains a file with a chan float64 iterable and another with a float64 slice iterable.

Usage example:

func main() {
  rounded, err := NewFloat64Iter(1.2, 2.4, 3.5, 5.6).Filter(func(n float64) bool {
		return n > 2.0
  }).Map(func(int i, n float64) interface{} {
    return int(n)
  }).ToInt()
  fmt.Println(rounded) // [3 5]
}

go-itergen's People

Contributors

erizocosmico avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  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.