Skip to content

Goom11/goGenerics

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 

Repository files navigation

Generics Generator for Go

Issue:

Currently Go has no support for generics

Hack:

This provides a temporary hack by providing all possible combinations of common functional list functions for Go Slices

Currently Implemented:

  1. Self (x => x)
  2. FoldLeft (list z function => result)
  3. Map (list function => newList)
  4. Filter (list predicate => newList)

How To Use:

  1. Change generator.go's global variable 'types' to include all the types you need functions for
  2. Generated functions will be of the form FunctionType1Type2Type3... (see example code for more info)
  3. Build and run, take output and use from personal package
  4. Enjoy, Contribute, Be Merry!

Examples:

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)
}

TODO:

  • (filter)/remove/partition
  • takeWhile/dropWhile/span
  • count
  • forall/exists
  • zip?
  • union?/intersect?

About

generics generator for golang

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages