github.com/l3x/learn-fp-go@v0.0.0-20171228022418-7639825d0b71/1-functional-fundamentals/ch03-hof/02_fcn_comp/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  type StrFunc func(string) string
     9  
    10  func Compose(f StrFunc, g StrFunc) StrFunc {
    11  	return func(s string) string {
    12  		return g(f(s))
    13  	}
    14  }
    15  
    16  func main() {
    17  	var recognize = func(name string) string {
    18  			return fmt.Sprintf("Hey %s", name)
    19  		}
    20  
    21  	var emphasize = func(statement string) string {
    22  		return fmt.Sprintf(strings.ToUpper(statement) + "!")
    23  		}
    24  
    25  	var greetFoG = Compose(recognize, emphasize)
    26  	fmt.Println(greetFoG("Gopher"))
    27  
    28  	var greetGoF = Compose(emphasize, recognize)
    29  	fmt.Println(greetGoF("Gopher"))
    30  }