86

GitHub - AdamColton/compose

 6 years ago
source link: https://github.com/AdamColton/compose
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Function Composition in Go

I did this more to see if I could than because it's a good idea. The compose.New function can take two or more functions and compose them. It checks that the output of one function matches the input of the next.

Getting the basic version working was pretty easy, it took a little work to get it working correctly with variadic functions.

The return value does need to be cast to a func type to be used

  rms := compose.New(squareSlice, avg, math.Sqrt).(func([]float64) float64)
  fmt.Println(rms(5,6,7)) // 6.055...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK