Skip to content

Instantly share code, notes, and snippets.

@nvasilakis
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save nvasilakis/2460d30257d6c001574b to your computer and use it in GitHub Desktop.

Select an option

Save nvasilakis/2460d30257d6c001574b to your computer and use it in GitHub Desktop.
package main
import "fmt"
func fmap( f (func(int) int), a []int) []int {
for i:= range a {
a[i] = f(a[i])
}
return a
}
// Doing fold with map
func adder() func(int) int {
sum := 0
return func(x int) int {
sum += x
return sum
}
}
func main() {
//pos, neg := adder(), adder()
weights := []int{1,2,3,4,5}
/*
for i := 0; i < 10; i++ {
fmt.Println(
pos(i),
neg(-2*i),
)
}*/
a := fmap(adder(),weights)
for i:= range a {
fmt.Println(a[i])
}
}
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
// 1 1 2 3 5 8 13 21
func fibonacci() func() int {
f1 := 1
f2:= 0
res :=0
return func() int {
res = f1 + f2
f2 = f1
f1 = res
return res
}
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment