From f3bf95e66c673031aeefd4cd3d85259397be965b Mon Sep 17 00:00:00 2001 From: Inanc Gumus Date: Thu, 2 May 2019 22:09:45 +0300 Subject: [PATCH] add: advanced funcs --- 27-functions-advanced/02-func-values/main.go | 52 ++++++++++ .../03-func-values-part-2/main.go | 66 +++++++++++++ .../04-func-literals/main.go | 63 ++++++++++++ .../05-func-literals-part-2/main.go | 97 +++++++++++++++++++ 27-functions-advanced/exercises/README.md | 8 ++ 5 files changed, 286 insertions(+) create mode 100644 27-functions-advanced/02-func-values/main.go create mode 100644 27-functions-advanced/03-func-values-part-2/main.go create mode 100644 27-functions-advanced/04-func-literals/main.go create mode 100644 27-functions-advanced/05-func-literals-part-2/main.go create mode 100644 27-functions-advanced/exercises/README.md diff --git a/27-functions-advanced/02-func-values/main.go b/27-functions-advanced/02-func-values/main.go new file mode 100644 index 0000000..e0f4ac4 --- /dev/null +++ b/27-functions-advanced/02-func-values/main.go @@ -0,0 +1,52 @@ +// For more tutorials: https://blog.learngoprogramming.com +// +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// + +package main + +import "fmt" + +type filterFunc func(int) bool + +func main() { + signatures() + funcValues() +} + +func isEven(n int) bool { + return n%2 == 0 +} + +func isOdd(m int) bool { + return m%2 == 1 +} + +func signatures() { + fmt.Println("••• FUNC SIGNATURES (TYPES) •••") + fmt.Printf("isEven : %T\n", isEven) + fmt.Printf("isOdd : %T\n", isOdd) +} + +func funcValues() { + fmt.Println("\n••• FUNC VALUES (VARS) •••") + + // var value func(int) bool + var value filterFunc + fmt.Printf("value nil? : %t\n", value == nil) + + value = isEven + fmt.Printf("value(2) : %t\n", value(2)) + fmt.Printf("isEven(2) : %t\n", isEven(2)) + + value = isOdd + fmt.Printf("value(1) : %t\n", value(1)) + fmt.Printf("isOdd(1) : %t\n", isOdd(1)) + + fmt.Printf("value nil? : %t\n", value == nil) + fmt.Printf("value : %p\n", value) + fmt.Printf("isEven : %p\n", isEven) + fmt.Printf("isOdd : %p\n", isOdd) +} diff --git a/27-functions-advanced/03-func-values-part-2/main.go b/27-functions-advanced/03-func-values-part-2/main.go new file mode 100644 index 0000000..4a7a24f --- /dev/null +++ b/27-functions-advanced/03-func-values-part-2/main.go @@ -0,0 +1,66 @@ +// For more tutorials: https://blog.learngoprogramming.com +// +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// + +package main + +import "fmt" + +type filterFunc func(int) bool + +func main() { + signatures() + + fmt.Println("\n••• WITHOUT FUNC VALUES •••") + nums := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} + + fmt.Printf("evens : %d\n", filterEvens(nums...)) + fmt.Printf("odds : %d\n", filterOdds(nums...)) + + fmt.Println("\n••• FUNC VALUES •••") + fmt.Printf("evens : %d\n", filter(isEven, nums...)) + fmt.Printf("odds : %d\n", filter(isOdd, nums...)) +} + +func filter(f filterFunc, nums ...int) (filtered []int) { + for _, n := range nums { + if f(n) { + filtered = append(filtered, n) + } + } + return +} +func filterOdds(nums ...int) (filtered []int) { + for _, n := range nums { + if isOdd(n) { + filtered = append(filtered, n) + } + } + return +} + +func filterEvens(nums ...int) (filtered []int) { + for _, n := range nums { + if isEven(n) { + filtered = append(filtered, n) + } + } + return +} + +func isEven(n int) bool { + return n%2 == 0 +} + +func isOdd(m int) bool { + return m%2 == 1 +} + +func signatures() { + fmt.Println("••• FUNC SIGNATURES (TYPES) •••") + fmt.Printf("isEven : %T\n", isEven) + fmt.Printf("isOdd : %T\n", isOdd) +} diff --git a/27-functions-advanced/04-func-literals/main.go b/27-functions-advanced/04-func-literals/main.go new file mode 100644 index 0000000..ca34763 --- /dev/null +++ b/27-functions-advanced/04-func-literals/main.go @@ -0,0 +1,63 @@ +// For more tutorials: https://blog.learngoprogramming.com +// +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// + +package main + +import "fmt" + +type filterFunc func(int) bool + +func main() { + nums := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} + + fmt.Println("••• FUNC VALUES •••") + fmt.Printf("evens : %d\n", filter(isEven, nums...)) + fmt.Printf("odds : %d\n", filter(isOdd, nums...)) + fmt.Printf("> 3 : %d\n", filter(greaterThan3, nums...)) + fmt.Printf("> 6 : %d\n", filter(greaterThan6, nums...)) + + // ======================================================================== + + fmt.Println("\n••• FUNC LITERALS •••") + + var min int + greaterThan := func(n int) bool { + return n > min + } + + min = 3 + fmt.Printf("> 3 : %d\n", filter(greaterThan, nums...)) + + min = 6 + fmt.Printf("> 6 : %d\n", filter(greaterThan, nums...)) + + // ======================================================================== + + fmt.Println("\n••• HIGHER-ORDER FUNCS •••") + fmt.Printf("> 3 : %d\n", filter(greater(3), nums...)) + fmt.Printf("> 6 : %d\n", filter(greater(6), nums...)) +} + +func greater(min int) filterFunc { + return func(n int) bool { + return n > min + } +} + +func greaterThan6(n int) bool { return n > 6 } +func greaterThan3(n int) bool { return n > 3 } +func isEven(n int) bool { return n%2 == 0 } +func isOdd(m int) bool { return m%2 == 1 } + +func filter(f filterFunc, nums ...int) (filtered []int) { + for _, n := range nums { + if f(n) { + filtered = append(filtered, n) + } + } + return +} diff --git a/27-functions-advanced/05-func-literals-part-2/main.go b/27-functions-advanced/05-func-literals-part-2/main.go new file mode 100644 index 0000000..9cbc127 --- /dev/null +++ b/27-functions-advanced/05-func-literals-part-2/main.go @@ -0,0 +1,97 @@ +// For more tutorials: https://blog.learngoprogramming.com +// +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// + +package main + +import ( + "fmt" +) + +type filterFunc func(int) bool + +func main() { + fmt.Println("••• HIGHER-ORDER FUNCS •••") + + nums := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} + + odd := reverse(reverse(isEven)) + fmt.Printf("reversed : %t\n", odd(8)) + + fmt.Printf("> 3 : %d\n", filter(greater(3), nums...)) + fmt.Printf("> 6 : %d\n", filter(greater(6), nums...)) + fmt.Printf("<= 6 : %d\n", filter(lesseq(6), nums...)) + fmt.Printf("<= 6 : %d\n", filter(reverse(greater(6)), nums...)) + + fmt.Println("\n••• CLOSURES •••") + fmt.Printf("uniques : %d\n", filter(uniques(), 1, 1, 2, 3, 3)) + + dups := reverse(uniques()) + fmt.Printf("dups : %v\n", filter(dups, 1, 1, 2, 3, 3)) + + dups = reverse(uniques()) + fmt.Printf("dups dups : %v\n", filter(dups, 1, 1, 2, 3, 3, 3, 3)) + + dups = reverse(uniques()) + out := filter(dups, 1, 1, 2, 3, 3, 3, 3) + fmt.Printf("dups uniqs : %v\n", filter(uniques(), out...)) + + dups = reverse(uniques()) + chained := chain(reverse(greater(2)), dups, uniques()) + fmt.Printf("dups chain : %v\n", filter(chained, 1, 1, 2, 3, 3, 3, 3, 0, 0)) +} + +func chain(filters ...filterFunc) filterFunc { + return func(n int) bool { + for _, next := range filters { + if !next(n) { + return false + } + } + return true + } +} + +func uniques() filterFunc { + seen := make(map[int]bool) + + return func(n int) (ok bool) { + if !seen[n] { + seen[n], ok = true, true + } + return + } +} + +func reverse(f filterFunc) filterFunc { + return func(n int) bool { + return !f(n) + } +} + +func greater(min int) filterFunc { + return func(n int) bool { + return n > min + } +} + +func filter(f filterFunc, nums ...int) (filtered []int) { + for _, n := range nums { + if f(n) { + filtered = append(filtered, n) + } + } + return +} + +func isEven(n int) bool { return n%2 == 0 } +func isOdd(m int) bool { return m%2 == 1 } + +func lesseq(max int) filterFunc { + return func(n int) bool { + return n <= max + } +} diff --git a/27-functions-advanced/exercises/README.md b/27-functions-advanced/exercises/README.md new file mode 100644 index 0000000..8b4725b --- /dev/null +++ b/27-functions-advanced/exercises/README.md @@ -0,0 +1,8 @@ +# IDEAS + +* Create a chainable funcs for strings +* Create a mapper instead of a filter +* Create map/reduce +* Create a time it takes higher-order func +* Use stdlib funcs with func values +* Create a functional program with structs (filter etc) \ No newline at end of file