Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
ty/fun/func.go
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
35 lines (30 sloc)
807 Bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fun | |
import ( | |
"reflect" | |
"github.com/BurntSushi/ty" | |
) | |
// Memo has a parametric type: | |
// | |
// func Memo(f func(A) B) func(A) B | |
// | |
// Memo memoizes any function of a single argument that returns a single value. | |
// The type `A` must be a Go type for which the comparison operators `==` and | |
// `!=` are fully defined (this rules out functions, maps and slices). | |
func Memo(f interface{}) interface{} { | |
chk := ty.Check( | |
new(func(func(ty.A) ty.B)), | |
f) | |
vf := chk.Args[0] | |
saved := make(map[interface{}]reflect.Value) | |
memo := func(in []reflect.Value) []reflect.Value { | |
val := in[0].Interface() | |
ret, ok := saved[val] | |
if ok { | |
return []reflect.Value{ret} | |
} | |
ret = call1(vf, in[0]) | |
saved[val] = ret | |
return []reflect.Value{ret} | |
} | |
return reflect.MakeFunc(vf.Type(), memo).Interface() | |
} |