publish function sequence
This commit is contained in:
parent
91c4866d20
commit
904a742d0c
@ -9,11 +9,11 @@ Arrays
|
||||
Slices
|
||||
Maps
|
||||
Range
|
||||
# Functions
|
||||
# Multiple Return Values
|
||||
Functions
|
||||
Multiple Return Values
|
||||
# Varadic Functions
|
||||
# Closures
|
||||
# Recursion
|
||||
Closures
|
||||
Recursion
|
||||
# Defer
|
||||
# Panic
|
||||
# Pointers
|
||||
|
@ -1,20 +1,40 @@
|
||||
// Go supports [_anonymous functions_](http://en.wikipedia.org/wiki/Anonymous_function),
|
||||
// which can form <a href="http://en.wikipedia.org/wiki/Closure_(computer_science)"><em>closures</em></a>.
|
||||
// Anonymous functions are useful when you want to define
|
||||
// a function inline without having to name it.
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func makeEvenGenerator() func() uint {
|
||||
i := uint(0)
|
||||
return func() uint {
|
||||
i += 2
|
||||
// This function `intSeq` returns another function, which
|
||||
// we define anonymously in the body of `intSeq`. The
|
||||
// returned function _closes over_ the variable `i` to
|
||||
// form a closure.
|
||||
func intSeq() func() int {
|
||||
i := 0
|
||||
return func() int {
|
||||
i += 1
|
||||
return i
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
nextEven := makeEvenGenerator()
|
||||
fmt.Println(nextEven())
|
||||
fmt.Println(nextEven())
|
||||
fmt.Println(nextEven())
|
||||
}
|
||||
|
||||
// todo: note example of first-class functions.
|
||||
// We call `intSeq`, assigning the result (a function)
|
||||
// to `nextInt`. This function value captures its
|
||||
// own `i` value, which will be updated each time
|
||||
// we call `nextInt`.
|
||||
nextInt := intSeq()
|
||||
|
||||
// See the effect of the closure by calling `nextInt`
|
||||
// a few times.
|
||||
fmt.Println(nextInt())
|
||||
fmt.Println(nextInt())
|
||||
fmt.Println(nextInt())
|
||||
|
||||
// To confirm that the state is unique to that
|
||||
// particular function, create and test a new one.
|
||||
newInts := intSeq()
|
||||
fmt.Println(newInts())
|
||||
}
|
||||
|
8
examples/closures/closures.sh
Normal file
8
examples/closures/closures.sh
Normal file
@ -0,0 +1,8 @@
|
||||
$ go run closures.go
|
||||
1
|
||||
2
|
||||
3
|
||||
1
|
||||
|
||||
# The last feature of functions we'll look at for now is
|
||||
# recursion.
|
@ -1,34 +1,26 @@
|
||||
// Funcations are critical in Go as in any other language.
|
||||
// We'll look at some basic examples first.
|
||||
// _Functions_ are central in Go. We'll learn about
|
||||
// functions with a few different examples.
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
// The syntax is `func name(args) returns { body }`. Note
|
||||
// the the types of arguments come after their name.
|
||||
// In this case we're taking a slice of floats and
|
||||
// returning a single float value.
|
||||
func avg(vals []float64) float64 {
|
||||
total := 0.0
|
||||
for _, val := range vals {
|
||||
total += val
|
||||
}
|
||||
// Go requires an expliciti return, i.e. it won't
|
||||
// Here's a function that takes two `int`s and returns
|
||||
// their sum as an `int`.
|
||||
func plus(a int, b int) int {
|
||||
|
||||
// Go requires explicit returns, i.e. it won't
|
||||
// automatically return the value of the last
|
||||
// expression.
|
||||
return total / float64(len(vals))
|
||||
return a + b
|
||||
}
|
||||
|
||||
func main() {
|
||||
input := []float64{98, 93, 77, 82, 83}
|
||||
fmt.Println(input)
|
||||
|
||||
// Call a function just as you'd expect, with
|
||||
// `name(args)`.
|
||||
output := avg(input)
|
||||
fmt.Println(output)
|
||||
res := plus(1, 2)
|
||||
fmt.Println("1+2 =", res)
|
||||
}
|
||||
|
||||
// There are several other features to Go functions,
|
||||
// including multiple return values and varargs, which
|
||||
// we'll look at next.
|
||||
// todo: coalesced parameter types
|
||||
|
5
examples/functions/functions.sh
Normal file
5
examples/functions/functions.sh
Normal file
@ -0,0 +1,5 @@
|
||||
$ go run functions.go
|
||||
1+2 = 3
|
||||
|
||||
# There are several other features to Go functions. One is
|
||||
# multiple return values, which we'll look at next.
|
@ -1,4 +1,4 @@
|
||||
// Go has built-in support for multiple return values.
|
||||
// Go has built-in support for _multiple return values_.
|
||||
// This feature is used often in idiomatic Go, for example
|
||||
// to return both result and error values from a function.
|
||||
|
||||
@ -6,15 +6,16 @@ package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
// The `(int, int)` in this signature shows that the
|
||||
// function returns 2 ints.
|
||||
// The `(int, int)` in this function signature shows that
|
||||
// the function returns 2 `int`s.
|
||||
func vals() (int, int) {
|
||||
return 3, 7
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
// Here we use the 2 different return values from the
|
||||
// call, i.e. perform multiple assignement.
|
||||
// call with _multiple assignment_.
|
||||
a, b := vals()
|
||||
fmt.Println(a)
|
||||
fmt.Println(b)
|
||||
@ -24,3 +25,6 @@ func main() {
|
||||
_, c := vals()
|
||||
fmt.Println(c)
|
||||
}
|
||||
|
||||
// todo: named return parameters
|
||||
// todo: naked returns
|
||||
|
@ -2,3 +2,6 @@ $ go run multiple-return-values.go
|
||||
3
|
||||
7
|
||||
7
|
||||
|
||||
# Another key aspect of functions in Go is their ability
|
||||
# to form closures, which we'll look at next.
|
||||
|
@ -1,14 +1,20 @@
|
||||
// Go supports
|
||||
// <a href="http://en.wikipedia.org/wiki/Recursion_(computer_science)"><em>recursive functions</em></a>.
|
||||
// Here's a classic factorial example.
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func factorial(x uint) uint {
|
||||
if x == 0 {
|
||||
// This `fact` function calls itself until it reaches the
|
||||
// base case of `fact(0)`.
|
||||
func fact(n int) int {
|
||||
if n == 0 {
|
||||
return 1
|
||||
}
|
||||
return x * factorial(x-1)
|
||||
return n * fact(n-1)
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(factorial(7))
|
||||
fmt.Println(fact(7))
|
||||
}
|
||||
|
2
examples/recursion/recursion.sh
Normal file
2
examples/recursion/recursion.sh
Normal file
@ -0,0 +1,2 @@
|
||||
$ go run recursion.go
|
||||
5040
|
Loading…
x
Reference in New Issue
Block a user