Last active
January 12, 2020 10:56
-
-
Save b10s/9c63abaf3c2113d3e0a73c67999a4297 to your computer and use it in GitHub Desktop.
is it possible to have one implementation of method for receiver which may be have multiple types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package strain | |
//Ints is an array of ints. | |
type Ints []int | |
//Strings is an array of strings. | |
type Strings []string | |
//Lists is an array of ints' arrays. | |
type Lists []Ints | |
//Discard returns a new collection containing those elements where the predicate is false. | |
func (l interface{}) Discard(p func(int) bool) (res interface{}) { | |
for _, i := range l { | |
if !p(i) { | |
res = append(res, i) | |
} | |
} | |
return res | |
} | |
//Keep returns a new collection containing those elements where the predicate is true. | |
func (l interface{}) Keep(p func(int) bool) (res interface{}) { | |
var res Ints | |
for _, i := range l { | |
if p(i) { | |
res = append(res, i) | |
} | |
} | |
return res | |
} | |
//Discard returns a new collection containing those elements where the predicate is false. | |
func (l Strings) Discard(p func(string) bool) Strings { | |
var res Strings | |
for _, i := range l { | |
if !p(i) { | |
res = append(res, i) | |
} | |
} | |
return res | |
} | |
//Keep returns a new collection containing those elements where the predicate is true. | |
func (l Strings) Keep(p func(string) bool) Strings { | |
var res Strings | |
for _, i := range l { | |
if p(i) { | |
res = append(res, i) | |
} | |
} | |
return res | |
} | |
//Discard returns a new collection containing those elements where the predicate is false. | |
func (l Lists) Discard(p func([]int) bool) Lists { | |
var res Lists | |
for _, i := range l { | |
if !p(i) { | |
res = append(res, i) | |
} | |
} | |
return res | |
} | |
//Keep returns a new collection containing those elements where the predicate is true. | |
func (l Lists) Keep(p func([]int) bool) Lists { | |
var res Lists | |
for _, i := range l { | |
if p(i) { | |
res = append(res, i) | |
} | |
} | |
return res | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment