add: advanced funcs

This commit is contained in:
Inanc Gumus
2019-05-02 22:09:45 +03:00
parent a189065c38
commit f3bf95e66c
5 changed files with 286 additions and 0 deletions

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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
}

View File

@ -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
}
}

View File

@ -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)