199 points · by realrocker · 13y ago · 88 comments
Instantly, and progressively, by starting at the top and working through a single document that is both a reference and a guide.
Beautiful work.
Edit: One change I would make is to change p := pair{3, 4} into p := pair{x: 3, y: 4} which helps your declarations keep working in the future when you extend or modify the definition of `pair`.
Well, pair is an anti-pattern anyway, but what exactly would you add to "Pair"? "File not found"? :)
So cons is an "anti-pattern" as well, then?
Your comment is the most original dis of Lisp I've ever read. Bravo!
Pair is an OO anti-pattern because it's an arbitrary grouping of two things in a generic container, and you shouldn't do that in OO - classes are cheap. You should make a class that represents what the collection of those two things actually are. Typical field names in generic Pair objects are 'left' and 'right' or 'first' and 'second' which tells you nothing about the values you stick into them (except the types, if you're using a strongly typed language). If, as suggested in the example, the pair holds a 'x' and a 'y', a better choice could be to call it "Point" or even "Point2D". If it's a key/value pair from a map data structure it should be "Entry" with fields "key" and "value" - etc.
Perhaps a var named "brillant"
I really hope to be able to write Go professionally at some point, it is a really nice language to write in.
(Go's multiple return values seem to just be simulating a single use case of proper tuples.)
Thanks.
class Default a where
def :: a
and an unwise pair of instances: instance Default Int where
def = 7
instance Default String where
def = "foo"
This means I can say: 3 + def + length ("c" ++ def)
and get back 14. Note that which "def" depends on the type expected where it is used.---
It's not limited to values, either. Consider the "return" function in the Monad typeclass:
return :: Monad m => a -> m a
which is a function that takes something and returns that something wrapped in a monad. Which monad? Whatever is expected at the call site. [1, 2, 3] ++ return 4
gives us [1, 2, 3, 4] because list is a monad and return for lists gives a single element list, whereas putStrLn "foo" >> return 4
gives us an IO action that, when executed, prints "foo" and yeilds a 4.---
A super complex example is variadic functions like printf, with the type
printf :: PrintfType r => String -> r
PrintfType can be a String or IO (), giving you something like C's sprintf or printf based on the call site (which is itself cool), but it can also be a function that takes an instance of PrintfArg and gives back some new PrintfType (in an interesting intersection with currying).One question: why have named return values (in learnMemory) if you return something different? If you didn't have a return statement at all, would the function return the final value of p and q?
Probably to avoid declaring the function as `func learnMemory() (int, int)`.
>If you didn't have a return statement at all, would the function return the final value of p and q?
No, you have to do that explicitly. You can do `return`, though, which will return the variables whose names match the definition.
> One question: why have named return values (in learnMemory) if you return something different? If you didn't have a return statement at all, would the function return the final value of p and q?
Kinda. You still have a return statement, but you may omit the arguments.
See: http://golang.org/doc/effective_go.html#named-resultsWhen you create a new F# project you can select F# Tutorial and it generates a working project with a source code file that is an introduction to the language and its features.
I wish more IDEs did this.
That said, I've already professed my love for the Go language, which I consider the spiritual successor of the C programming language.
Fortunately you can still have fun on the JVM, even though I prefer to avoid it these days given its current custodians.
Because I can't see any difference in:
package main
import (
"fmt"
)
func main() {
m1 := map[string]int{}
m1["a"] = 1
fmt.Printf("%d\n", m1["a"])
m2 := make(map[string]int)
m2["b"] = 2
fmt.Printf("%d\n", m2["b"])
} func main() {
var m1 map[string]int
m1["a"] = 1
fmt.Printf("%d\n", m1["a"])
}
http://play.golang.org/p/JJQ7dSZLeAwhich yields
panic: runtime error: assignment to entry in nil map type pair struct {
x, y int
}
func main() {
var p pair
fmt.Printf("%d\n", p.x)
fmt.Printf("%d\n", p.y)
}
That's just wrong, that declaration and initialization behaves differently depending on the type.It would be my choice because unlike a lot of other pet ideas which would radically change Go into a new language, I'm pretty sure this would have hardly any effect... excepting of course to remove the Billion Dollar Mistake from the next up-and-coming language.
m1 := map[string]int{}
m1["a"] = 1http://golang.org/doc/effective_go.html?ModPagespeed=noscrip...
Yes, that is how you satisfy an interface in Go.
>What if you have interfaces with similar names?
The problem of interfaces with similar names is actually no different than the problems of, for example, packages or functions having similar names. It's down to you to avoid naming things in a confusing manner.
Seriously thinking about writing the Django equivalent of Michael Hartl's tutorial for Rails. Intimidating as it's a lot of work, over 100k words. Any people would like this? I really could not find a Django/Python equivalent.
Uh, I think it is. I can't go three articles without seeing Go mentioned.
I'm going to get downmodded to gray, but either this tutorial is missing something or Go is way over-hyped. I saw nothing special in the language at all, I have no idea what all the obsession is about lately.
EDIT: Also "// x == 1 here." should be "// x == 2 here.", those are 3 iterations.
Idiomatic Go code almost never uses multi-line comments, see: http://golang.org/src/pkg/net/http/request.go
> Also "// x == 1 here." should be "// x == 2 here."
No, it's correct. := declares a new variable. The newly declared, inner x shadows the outer x only within the block scope of the for loop:
And the usual amazon pricing: new $26, used $35.
fmt.Println("it's a", i)
Should be something like:
fmt.Println("it's a %T", i)
See http://golang.org/pkg/fmt/ for more options.
[update]
Forgot they allowed to send pull requests; done :)
I figured they ment to do the latter, since the next print function outputs: "it's a string".
// If statements require brace brackets, and do not require parens.
Even if it is single line?This is one of the strong points of go (whether you like it or hate it): gofmt imposes the same formatting rules for everyone using the language. It's not a choice for you (or your project) to make. Period.
This way you can't go wrong adding another statement inside the if.