You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
727 B
38 lines
727 B
package main
|
|
|
|
import "fmt"
|
|
|
|
func main() {
|
|
zero, one, two, three := 0, 1, 2, 3
|
|
list := []*int{&one, &two, nil, &three, &zero}
|
|
|
|
// this will work, as the values will be evaluated
|
|
// "lazily" - the nested functions will never
|
|
// be executed, thus it will never panic.
|
|
fmt.Printf("%v\n", myFold(mulLazy, 1, list))
|
|
// This will panic.
|
|
fmt.Printf("%v\n", foldl(mul, 1, list))
|
|
}
|
|
|
|
func mul(x int, y *int) int {
|
|
if *y == 0 {
|
|
return 0
|
|
}
|
|
return x * *y
|
|
}
|
|
|
|
func mulLazy(x func() int, y *int) func() int {
|
|
return func() int {
|
|
if *y == 0 {
|
|
return 0
|
|
}
|
|
return x() * *y
|
|
}
|
|
}
|
|
|
|
func myFold(f func(func() int, *int) func() int, acc int, list []*int) int {
|
|
a := func() int { return acc }
|
|
a = foldl(f, a, list)
|
|
return a()
|
|
}
|