fungen is a tool that can be used with go generate
to automate the creation of list like types and methods like Map, Filter, Reduce, etc on these types.
This allows you to use constructs like the following on lists of any types:
myList.Map(func(t type1) type1 {
...
}).Filter(func(t type1) bool {
...
}).Take(3).Reduce(initialValue type1, func (prev type1, current type1) type1 {
...
})
The Map
and Filter
methods have parallel counterparts PMap
and PFilter
which can be used to parallelize time consuming operations.
Note: This tool uses standard golang constructs to make this functionality available on any type - standard and custom. Interfaces have NOT been used.
While the for
loop in Go is quite nice, it still involves some boilerplate code. In my particular case, I realized that I could eliminate a majority of that code by using the following constructs:
Assuming you have installed fungen
(see below), you can start by including the comment in your file for go generate
to process. Let's assume we want to use the above methods with a list of strings (type []string).
Add the following comment in your source code:
//go:generate fungen -types string
Now run the go generate
command in the same directory where this source file is located. This will generate a file named fungen_auto.go
in this directory. This file will contain the type and method definitions that you'll need.
If you take a look at the file, you'll see that a new type has been created:
type stringList []string
If you now use this type (stringList
) in place of []string
, you'll be able to use all the above mentioned methods on that list of string.
words := stringList{"calculator", "skip", "grape", "obtainable", "inexpensive", "panoramic", "powerful"}
wordsWithLengthLessThan7 := words.Filter(func(w string) bool {
return len(w) < 7
})
// wordsWithLengthLessThan7 = [skip grape]
go install github.com/kulshekhar/fungen
The complete command to generate the code:
fungen -package PackageName -types comma,Separated,Types,With,Optional:Opt,short:Sh,names:n
//go:generate fungen -types string,int
followed by
go generate
on the command line, in the directory where you wish the generated file to be stored.
fungen -types string,int
This tool will generate as a file named fungen_auto.go
.
-package PackageName
The -package
parameter is optional. If specified, it will generate the code file by using package PackageName
at the top. If omitted, it will generate the code file by using package main
at the top.
-types comma,Separated,Types,With,Optional:Opt,short:Sh,names:n
The -types
parameter takes a comma separated list of types for which the list type and methods on this type should be generated. eg: (int,string,uint,customType)
Each of the comma separated values can themselves optionally be a colon separated value. If this is the case, the first part (before the colon) should be a valid type name (built in or custom) and the second is the name used in the names of the methods.
-filename filename.go
Filename for generated package (default "fungen_auto.go"). The -filename
parameter is optional.
-methods Map,Filter
Comma separated list of methods to generate. By default generate all methods.
Valid methods is: Map,PMap,Filter,PFilter,Reduce,ReduceRight,Each,EachI,Take,TakeWhile,Drop,DropWhile,All,Any,FilterMap,PFilterMap
If -types int,string
is used, the types generated will be:
type intList []int
type stringList []string
The following methods will be available on both these types:
Map
PMap
Filter
PFilter
Reduce
ReduceRight
Take
TakeWhile
Drop
DropWhile
Each
EachI
All
Any
Additionally, the intList
type will have the following methods:
MapString
PMapString
FilterMapString
PFilterMapString
And the stringList
type will have the following methods:
MapInt
PMapInt
FilterMapInt
PFilterMapInt
If -types int:I,string:Str
is used, the names of the common set of methods generated will be the same as above. However, the types and the additional methods generated will have different names.
The types will be named as follows:
type IList []int
type StrList []string
The IList
type will have the following methods:
MapStr
PMapStr
And the StrList
type will have the following methods:
MapI
PMapI