To view parent comment, click here.
To read all comments associated with this story, please click here.
To be honest, that doesn't even look valid Go (though, as I said, I'm only a week in on the language).
However some of the weirder syntax I've read on the tutorials were purely demonstrations showing how the more advanced C++ routines would be ported (so not really your typical day to day functions).
But lets be honest, you can find extreme cases of bad code in any language, so if you're genuinely curious about the language then I'd recommend have a quick browse through the tour: http://tour.golang.org/
It takes time to adjust, but Go's declaration syntax is novel and helpful, especially when dealing with complex types.
Go:
f func(func(int,int) int, int) int
C#:
var f = Func<Func<int, int, int>, int, int>
There is just no sane way to write the syntax generally. I'm scared to even think what this would look like in C++.
You've been space butchered, but it's not that weird.
The anonymous function is simply a closure that captures the variables a and b, then modifies them both to produce the next Fibonacci number when called.
It's really quite standard. I mean compare to a similar Scheme function (mind you it's been awhile):
(define fib (let ((a 0) (b1))
(lambda () (begin (set! a b) (set! b (+ b 1)) a)))
But of course, until you share what language you think does this "better," it's impossible to address your complaint.
Edited 2013-01-23 22:19 UTC
I can read the code:
func fib() func() int {
declares a function that returns a function.
a, b := 0, 1
declares a and b as integer then initialized them with a zero.
return func() int {
a, b = b, a+b
return a
}
}
That's the closure, but the difference with its counterparts like Javascript and C# is that you can mix that example with pointers and a weird array initialization syntax that Go allows.
I said Go is a C like language, why do you keep comparing it with Javascript and C#?
Congrats, you've written shit code with no sense of style and taste. Hope you feel good that even automatic code formatting tools like "indent" are smarter than you. Writing unreadable code to make a statement about style is about as valid as criticizing car safety by driving down a mountain road at 200+mph, IOW nobody will take you seriously.





Member since:
2005-09-27
Personally, I find the sintax cryptic and I would only use it as a last resource.
For example:
func fib() func() int {
a, b := 0, 1
return func() int {
a, b = b, a+b
return a
}
}
Some people may feel comfortable with that kind of syntax, I don't, It is as bad as the worse C++ IMHO, well not that bad.
Edited 2013-01-23 20:58 UTC