| 
									
										
										
										
											2019-05-02 22:09:45 +03:00
										 |  |  | // 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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-03 23:04:56 +03:00
										 |  |  | import ( | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"strings" | 
					
						
							|  |  |  | 	"unicode" | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2019-05-02 22:09:45 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 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...)) | 
					
						
							| 
									
										
										
										
											2019-05-03 23:04:56 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	fmt.Println("\n••• MAPPING •••") | 
					
						
							|  |  |  | 	fmt.Println(strings.Map(unpunct, "hello!!! HOW ARE YOU???? :))")) | 
					
						
							|  |  |  | 	fmt.Println(strings.Map(unpunct, "TIME IS UP!")) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func unpunct(r rune) rune { | 
					
						
							|  |  |  | 	if unicode.IsPunct(r) { | 
					
						
							|  |  |  | 		return -1 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return unicode.ToLower(r) | 
					
						
							| 
									
										
										
										
											2019-05-02 22:09:45 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func filter(f filterFunc, nums ...int) (filtered []int) { | 
					
						
							|  |  |  | 	for _, n := range nums { | 
					
						
							|  |  |  | 		if f(n) { | 
					
						
							|  |  |  | 			filtered = append(filtered, n) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-05-03 23:04:56 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-02 22:09:45 +03:00
										 |  |  | 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) | 
					
						
							|  |  |  | } |