Currently Go has no support for generics
This provides a temporary hack by providing all possible combinations of common functional list functions for Go Slices
- Self (x => x)
- FoldLeft (list z function => result)
- Map (list function => newList)
- Filter (list predicate => newList)
- Change generator.go's global variable 'types' to include all the types you need functions for
- Generated functions will be of the form FunctionType1Type2Type3... (see example code for more info)
- Build and run, take output and use from personal package
- Enjoy, Contribute, Be Merry!
import "generics"
func addStrLen(x int, y string) int {
return x + len(y)
}
//Format:
//FoldLeftType1Type2 : f(Type2, Type1) -> Type2
func sumStringLengths(l []string) int {
return generics.FoldLeftstringint(l, 0, addStrLen)
}
func add(x int, y int) int {
return x + y
}
func sum(l []int) int {
return generics.FoldLeftintint(l, 0, add)
}
func mul(x int, y int) int {
return x * y
}
func product(l []int) int {
return generics.FoldLeftintint(l, 1, mul)
}
func double(x int) int {
return 2 * x
}
func doubleList(x []int) []int {
return generics.Mapintint(x, double)
}
func length(x string) int {
return len(x)
}
func stringsToLengths(x []string) []int {
return generics.Mapstringint(x, length)
}
func isEven(x int) bool {
return x % 2 == 0
}
func getEvens(l []int) []int {
return generics.Filterint(l, isEven)
}
- (filter)/remove/partition
- takeWhile/dropWhile/span
- count
- forall/exists
- zip?
- union?/intersect?