1

GitHub - calebcase/curry

 2 years ago
source link: https://github.com/calebcase/curry
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.

Curry

Curry provides facilities for currying Go functions. The process converts a single function like f(x, y, z) into N many functions each taking a single argument like h(x)(y)(z).

Currying can be useful as a way to provide partial function application. For example, this takes a function that adds two integers and creates a new function plus1 that increments the given value by 1.

package main

import (
  "fmt"

  "github.com/calebcase/curry"
)

func Add(x, y int) int {
  return x + y
}

func main() {
  // Create a reusable function with the first argument bound to 1.
  plus1 := curry.A2R1(Add)(1)

  fmt.Println(plus1(2)) // 3
  fmt.Println(plus1(3)) // 4
  fmt.Println(plus1(4)) // 5
}

For more examples and usage see the go docs.

Build

The library is entirely generated with jennifer:

go generate
go test -v


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK