Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "iter"
- "slices"
- )
- func Filter[T any](s []T, f func(T) bool) iter.Seq[T] {
- return func(yield func(T) bool) {
- for _, v := range s {
- if f(v) {
- if !yield(v) {
- return
- }
- }
- }
- }
- }
- func main() {
- var s = []string{"a", "bc", "d", "ef"}
- s = slices.Collect(Filter(s, func(s string) bool { return len(s) > 1 }))
- fmt.Println(s)
- }
Advertisement
Add Comment
Please, Sign In to add comment