package main
import "fmt"
type TransformerFunction func(int) int
func NewCompositeTransformer(f, g TransformerFunction) TransformerFunction {
return func(x int) int {
return f(g(x))
}
}
func Compose(f, g TransformerFunction, fs ...TransformerFunction) TransformerFunction {
composite := NewCompositeTransformer(f, g)
for _, f := range fs {
composite = NewCompositeTransformer(composite, f)
}
return composite
}
func double(x int) int {
return 2 * x
}
func inc(x int) int {
return x + 1
}
func square(x int) int {
return x * x
}
func main() {
doubleThenIncThenSquare := Compose(square, inc, double)
fmt.Println(doubleThenIncThenSquare(4))
}
cGFja2FnZSBtYWluCgppbXBvcnQgImZtdCIKCnR5cGUgVHJhbnNmb3JtZXJGdW5jdGlvbiBmdW5jKGludCkgaW50CgpmdW5jIE5ld0NvbXBvc2l0ZVRyYW5zZm9ybWVyKGYsIGcgVHJhbnNmb3JtZXJGdW5jdGlvbikgVHJhbnNmb3JtZXJGdW5jdGlvbiB7CglyZXR1cm4gZnVuYyh4IGludCkgaW50IHsKCQlyZXR1cm4gZihnKHgpKQoJfQp9CgpmdW5jIENvbXBvc2UoZiwgZyBUcmFuc2Zvcm1lckZ1bmN0aW9uLCBmcyAuLi5UcmFuc2Zvcm1lckZ1bmN0aW9uKSBUcmFuc2Zvcm1lckZ1bmN0aW9uIHsKCWNvbXBvc2l0ZSA6PSBOZXdDb21wb3NpdGVUcmFuc2Zvcm1lcihmLCBnKQoKCWZvciBfLCBmIDo9IHJhbmdlIGZzIHsKCQljb21wb3NpdGUgPSBOZXdDb21wb3NpdGVUcmFuc2Zvcm1lcihjb21wb3NpdGUsIGYpCgl9CgoJcmV0dXJuIGNvbXBvc2l0ZQp9CgpmdW5jIGRvdWJsZSh4IGludCkgaW50IHsKCXJldHVybiAyICogeAp9CgpmdW5jIGluYyh4IGludCkgaW50IHsKCXJldHVybiB4ICsgMQp9CgpmdW5jIHNxdWFyZSh4IGludCkgaW50IHsKCXJldHVybiB4ICogeAp9CgpmdW5jIG1haW4oKSB7Cglkb3VibGVUaGVuSW5jVGhlblNxdWFyZSA6PSBDb21wb3NlKHNxdWFyZSwgaW5jLCBkb3VibGUpCglmbXQuUHJpbnRsbihkb3VibGVUaGVuSW5jVGhlblNxdWFyZSg0KSkKfQ==