Generic Data Structures/Algorithms in golang.
Generic Data Structures/Algorithms in golang via code generation (glorified copy&paste).
One might want to use goinline tool to generate specific implementation of generic data structures, such as map, vector, matrix, set. Or to generate specific implementation of generic algorithms, such as sort, binary search.
Goals:
Install goinline.
go get github.com/sasha-s/go-inline/goinline/cmd/goinline
Blueprint is a package that implements a certain generic data type/algorithm.
Let us consider Vector[Number].
package vector
type Number float64
type Vector []Number
func (v Vector) Sum() Number {
var s Number
for _, x := range v {
s += x
}
return s
}
func (v Vector) Dot(w Vector) Number {
var s Number
for i, x := range v {
s += x * w[i]
}
return s
}
...
Number is a type parameter: we want to be able to use derived versions Vector[float32] or Vector[int].
Note, that Number is a standalone type. This makes it easy to generate the derived versions.
Since we are using +
and *
the Vector blueprint can be only used with ints and floats.
Have a look at more complete example
In case of Vector[Number]:
goinline -package=github.com/sasha-s/go-inline/examples/blueprints/vector --target-package-name=v -target-dir=. -w "Number->int"
This will use a blueprint from package github.com/sasha-s/go-inline/examples/blueprints/vector
, looking for the it in $GOPATH
, create a generated version in current (.
) folder, will rename the package to v
and will replace Number
with int
in the generated code.
Equivalent go:generate directive: //go:generate goinline -package=github.com/sasha-s/go-inline/examples/blueprints/vector --target-package-name=v -target-dir=. -w Number->int
See vector example.
Note, goinline does not check if the blueprint code compiles. Garbage in, garbage out.
Writing tests so they work after inlining types is tricky, so goinline does not generate tests by default.
One could get similar results with some scripting around gofmt -r, given that the blueprints are well-structured.