Skip to content

Instantly share code, notes, and snippets.

@b10s
Last active January 12, 2020 10:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b10s/9c63abaf3c2113d3e0a73c67999a4297 to your computer and use it in GitHub Desktop.
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
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