How do I declare an array of functions?

10,452 views
Skip to first unread message

micheles

unread,
Nov 13, 2009, 11:00:33 PM11/13/09
to golang-nuts
I have looked (briefly) at the docs, but I cannot find how to declare
an array of functions, how
to declare an higher order function taking a function in input or
output, and things like that.
Any help?

Edward Marshall

unread,
Nov 13, 2009, 11:11:51 PM11/13/09
to golang-nuts
On Fri, Nov 13, 2009 at 10:00 PM, micheles <michele....@gmail.com> wrote:
I have looked (briefly) at the docs, but I cannot find how to declare
an array of functions

package main
import "fmt";
func do_stuff() {
    fmt.Printf("Hello, world!\n");
}
func main() {
    flist := []func(){do_stuff};
    flist[0]();
}

Just a quick example.

--
Ed Marshall <e...@logic.net>
Felix qui potuit rerum cognoscere causas.
http://esm.logic.net/

Nigel Tao

unread,
Nov 13, 2009, 11:14:47 PM11/13/09
to golang-nuts
2009/11/14 micheles <michele....@gmail.com>:
For your second question,


package main

import "fmt"

func z(i int) int {
return i*100;
}

func foo(bar func(int) int) (func(int) int) {
x := bar(1);
return func(y int) int {
return x + y;
};
}

func main() {
a := foo(z);
fmt.Printf("hello %v\n", a(2));
}

micheles

unread,
Nov 14, 2009, 12:58:01 AM11/14/09
to golang-nuts
Thanks. I have also discovered vectors now.
I was trying to translate the following Python code (for educational
purposes about
the scoping rules):

for f in [lambda : i for i in range(3)]:
print f() # prints 2,2,2

This is my first attempt in Go:

package main

import (
"fmt";
vec "container/vector";
)

func make_vector(f func() int, len int) *vec.Vector {
v := vec.New(len);
for i := 0; i < len; i++ {
v.Set(i, f)
}
return v;
}

func main() {
N := 3;
a := make_vector(func() int { return 0 }, N);
for i := 0; i < N; i++ {
a.Set(i, func() int { return i })
}
for i := 0; i < N; i++ {
fmt.Printf("%d\n", a.At(i).(func() int)())
}
} // prints 3,3,3

I was not able to remove the type assertion. Is there a way to make
this better (shorter, more general?)

Nigel Tao

unread,
Nov 14, 2009, 2:45:52 AM11/14/09
to golang-nuts
2009/11/14 micheles <michele....@gmail.com>:
By "type assertion" I presume you mean the (func() int) conversion the
the third-last line:
fmt.Printf("%d\n", a.At(i).(func() int)())

You can't remove it yet, because go does not have generics yet.
http://golang.org/doc/go_lang_faq.html#generics


As for making it shorter, it certainly can be shorter, but I'm not
sure exactly what you're trying to do. For example,

a := make_vector(func() int { return 0 }, N);

sets all three elements of the vector to be the same "always return
zero" function. But in the very next bit of code

for i := 0; i < N; i++ {
a.Set(i, func() int { return i })
}

You're setting all three elements of that very same vector, i.e.
you're overwriting what you just did. So you could make your code
shorter by removing the make_vector call and just having

a := vec.New(N);


Also, if you were wondering, the program prints "3\n3\n3\n" because
the functions you set all close over the same variable i (the i of the
first for loop in main(), which is not the same i as the one in the
second for loop). If you want the program to print "0\n1\n2\n" then
replace the first for loop by

for i := 0; i < N; i++ {
j := i;
a.Set(i, func() int { return j })
}

so that the anonymous function (i.e. a closure) closes over a
different j each time.


Final nit: for a local variable, I'd use n rather than N.

manatlan

unread,
Nov 14, 2009, 5:27:22 AM11/14/09
to golang-nuts
use vector ! ... amazing

package main

import "container/vector"

func main() {
ll:= vector.New(0);
ll.Push( func() string { return ""} );
Reply all
Reply to author
Forward
0 new messages