Currying: Difference between revisions

1,242 bytes added ,  9 years ago
Add Go example
(→‎{{header|jq}}: fix typo)
(Add Go example)
Line 118:
> add2 7;;
val it : int = 9</lang>
 
=={{header|Go}}==
Go has had [http://golang.org/ref/spec#Function_literals function literals]
and [http://golang.org/ref/spec#Method_expressions method expressions]
since before Go 1.0.
[http://golang.org/ref/spec#Method_values Method values] were added
in [http://golang.org/doc/go1.1#method_values Go 1.1].
<lang go>package main
 
import (
"fmt"
"math"
)
 
func PowN(b float64) func(float64) float64 {
return func(e float64) float64 { return math.Pow(b, e) }
}
 
func PowE(e float64) func(float64) float64 {
return func(b float64) float64 { return math.Pow(b, e) }
}
 
type Foo int
 
func (f Foo) Method(b int) int {
return int(f) + b
}
 
func main() {
pow2 := PowN(2)
cube := PowE(3)
 
fmt.Println("2^8 =", pow2(8))
fmt.Println("4³ =", cube(4))
 
var a Foo = 2
fn1 := a.Method // A "method value", like currying 'a'
fn2 := Foo.Method // A "method expression", like uncurrying
 
fmt.Println("2 + 2 =", a.Method(2)) // regular method call
fmt.Println("2 + 3 =", fn1(3))
fmt.Println("2 + 4 =", fn2(a, 4))
fmt.Println("3 + 5 =", fn2(Foo(3), 5))
}</lang>
[http://play.golang.org/p/0YL9YTe-9V Run on the Go Playground.]
 
=={{header|Haskell}}==
Anonymous user