552ad88141c4f4f35aeb2977ef09eb4b176a2a6a — kotrunga 5 months ago 0fd71ac
completed the entire basics section of the tour (chapter 1)
M basics/structs-slices-maps/1.3.23_exercise-maps.go => basics/structs-slices-maps/1.3.23_exercise-maps.go +3 -3
@@ 17,11 17,11 @@ )
  
  func WordCount(s string) map[string]int {
- 	words := strings.Fields(s)
+ 	words := strings.Fields(s)			// splits the string s into a string array, splitting by spaces, testing with 'unicode.IsSpace'
  	wordCount := make(map[string]int)	// expanded: var wordCount map[string]int = make(map[string]int)
  	
- 	for _, word := range words {
- 		_, exists := wordCount[word]
+ 	for _, word := range words {		// range through each word in 'words'
+ 		_, exists := wordCount[word]	// wordCount is the map (the keys are strings, values are ints), get if the word exists in the map
  		
  		if exists {
  			wordCount[word] = wordCount[word] + 1

A basics/structs-slices-maps/1.3.24_function-values.go => basics/structs-slices-maps/1.3.24_function-values.go +27 -0
@@ 0,0 1,27 @@
+ /*
+ 	Notes 1.3.24
+ 
+ 	In Go, functions are also values. They can be passed to other functions, etc.
+ 	They can be used as function arguments and return values.
+ */
+ 
+ package main
+ 
+ import (
+ 	"fmt"
+ 	"math"
+ )
+ 
+ func compute(fn func(float64, float64) float64) float64 {
+ 	return fn(3,4)
+ }
+ 
+ func main() {
+ 	hypot := func(x, y float64) float64 {
+ 		return math.Sqrt(x*x + y*y)
+ 	}
+ 	fmt.Println(hypot(5, 12))
+ 
+ 	fmt.Println(compute(hypot))
+ 	fmt.Println(compute(math.Pow))
+ }< 
\ No newline at end of file

A basics/structs-slices-maps/1.3.25_function-closures.go => basics/structs-slices-maps/1.3.25_function-closures.go +32 -0
@@ 0,0 1,32 @@
+ /*
+ 	Notes 1.3.25
+ 
+ 	Go functions can be closures!
+ 
+ 	Reminder- a closure is a function value that references variables from outside it's own function body.
+ 	It may access and assign these referenced variables- so, the function is "bound" to the variables.
+ 
+ 	Below, the adder() function returns a closure. Each closure is bound to its own sum variable.
+ */
+ 
+ package main
+ 
+ import "fmt"
+ 
+ func adder() func(int) int {
+ 	sum := 0
+ 	return func(x int) int {
+ 		sum += x
+ 		return sum
+ 	}
+ }
+ 
+ func main() {
+ 	pos, neg := adder(), adder()
+ 	for i := 0; i < 10; i++ {
+ 		fmt.Println(
+ 			pos(i),
+ 			neg(-2 * i),
+ 		)
+ 	}
+ }< 
\ No newline at end of file

A basics/structs-slices-maps/1.3.26_exercise-fibonacci-closure.go => basics/structs-slices-maps/1.3.26_exercise-fibonacci-closure.go +66 -0
@@ 0,0 1,66 @@
+ /*
+ 	Notes 1.3.26
+ 
+ 	In this exercise, we implement a fibonacci function that returns a closure that
+ 	returns successive fibonacci numbres.
+ 
+ 	fibonacciWithSlices & fibonacciSimple are functions that return
+ 	a function that returns an int.
+ 
+ 	0, 1, 1, 2, 3, 5 ...
+ */
+ 
+ package main
+ 
+ import "fmt"
+ 
+ func fibonacciWithSlices() func() int {
+ 	var nums []int = make([]int, 0, 0)
+ 
+ 	return func() int {
+ 		var toReturn int
+ 
+ 		switch len(nums) {
+ 		case 0:
+ 			nums = append(nums, 0)
+ 			toReturn =  nums[0]
+ 		case 1:
+ 			nums = append(nums, 1)
+ 			toReturn =  nums[1]
+ 		default:
+ 			nums = append(nums, nums[len(nums) - 2] + nums[len(nums) - 1])
+ 			toReturn = nums[len(nums) - 1]
+ 		}
+ 
+ 		return toReturn
+ 	}
+ }
+ 
+ func fibonacciSimple() func() int {
+ 	n := 0
+ 	a := 0
+ 	b := 1
+ 	c := 0
+ 	
+ 	return func() int {
+ 		if n == 0 {
+ 			n++
+ 			return 0
+ 		} else if n == 1 {
+ 			n++
+ 			return 1
+ 		} else {
+ 			c = a + b
+ 			a = b
+ 			b = c
+ 			return c
+ 		}
+ 	}
+ }
+ 
+ func main() {
+ 	f := fibonacciSimple()
+ 	for i := 0; i < 10; i++ {
+ 		fmt.Println(f())
+ 	}
+ }< 
\ No newline at end of file