First-class Functions?

636 views
Skip to first unread message

Peter Thatcher

unread,
Nov 11, 2009, 11:18:51 AM11/11/09
to golang-nuts
First, let me say that I'm very excited to see Rob Pike reviving CSP.
But I have a question regarding first-class functions.

I read just about all of the documentation, and found no mention of
first-class functions. However, I saw this:

http://golang.org/src/pkg/once/once.go

func Do(f func()) {
// locking stuff
f();
// unlocking stuff
}

So, obviously there are first-class functions. Where are they
documented? What are the semantics? Are there anonymous functions?

Thank you to anyone that can answer this.

Adam Langley

unread,
Nov 11, 2009, 11:37:47 AM11/11/09
to Peter Thatcher, golang-nuts
On Wed, Nov 11, 2009 at 8:18 AM, Peter Thatcher <ptha...@gmail.com> wrote:
> So, obviously there are first-class functions.  Where are they
> documented?  What are the semantics?  Are there anonymous functions?

They work just the same at the top level as they do inside a function:

http://golang.org/doc/go_spec.html#Function_declarations

func f() {
g(func () int { return 1; });
}

And, yes, they are closures.


AGL

Peter Thatcher

unread,
Nov 11, 2009, 11:50:12 AM11/11/09
to golang-nuts
Thanks for that. I would recommend that an example like that get
added to the tutorial or other documentation. I'm sure I won't be the
last to ask :).

Oh, and since there are first-class functions, are there any libraries
for doing all of the typical functional things, like map, reduce,
scan, filter, etc?

On Nov 11, 9:37 am, Adam Langley <a...@golang.org> wrote:

Adam Langley

unread,
Nov 11, 2009, 12:02:32 PM11/11/09
to Peter Thatcher, golang-nuts
On Wed, Nov 11, 2009 at 8:50 AM, Peter Thatcher <ptha...@gmail.com> wrote:
> Oh, and since there are first-class functions, are there any libraries
> for doing all of the typical functional things, like map, reduce,
> scan, filter, etc?

The type system rather preludes them. They would either have to use
reflection (slow) or be specialised for each type of slice.


AGL

John Cowan

unread,
Nov 11, 2009, 12:48:17 PM11/11/09
to Adam Langley, Peter Thatcher, golang-nuts

The compiler could resolve the polymorphism locally into a monomorphic
operation, like Fortran or GNU C intrinsic functions (which are
polymorphic over different real and complex types). That would
require hacking the compiler or adding hygienic macros. In essence
they'd be new syntax rather than functions.

>
>
> AGL
>

--
GMail doesn't have rotating .sigs, but you can see mine at
http://www.ccil.org/~cowan/signatures

Ian Lance Taylor

unread,
Nov 12, 2009, 12:48:33 AM11/12/09
to Peter Thatcher, golang-nuts
Peter Thatcher <ptha...@gmail.com> writes:

> Thanks for that. I would recommend that an example like that get
> added to the tutorial or other documentation. I'm sure I won't be the
> last to ask :).

There are some examples in the Effective Go document.

> Oh, and since there are first-class functions, are there any libraries
> for doing all of the typical functional things, like map, reduce,
> scan, filter, etc?

There is no general library. There is vector.Do.

Ian
Reply all
Reply to author
Forward
0 new messages