forked from robpike/filter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apply.go2
86 lines (79 loc) · 2.75 KB
/
apply.go2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package filter contains utility functions for filtering slices through the
// distributed application of a filter function.
//
// The package is an experiment to see how easy it is to write such things
// in Go. It is easy, but for loops are just as easy and more efficient.
//
// You should not use this package.
//
package filter // import "robpike.io/filter"
// Apply takes a slice of type []T and a function of type func(T) U. (If the
// input conditions are not satisfied, Apply panics.) It returns a newly
// allocated slice where each element is the result of calling the function on
// successive elements of the slice.
func Apply[T, U any](slice []T, function func(T) U) []U {
out := make([]U, len(slice))
for i, x := range slice {
out[i] = function(x)
}
return out
}
// ApplyInPlace is like Apply, but overwrites the slice rather than returning a
// newly allocated slice.
func ApplyInPlace[T any](slice []T, function func(T) T) {
for i, x := range slice {
slice[i] = function(x)
}
}
// Choose takes a slice of type []T and a function of type func(T) bool. It returns a newly
// allocated slice containing only those elements of the input slice that
// satisfy the function.
func Choose[T any](slice []T, function func(T) bool) []T {
out := make([]T, 0)
for _, x := range slice {
if function(x) {
out = append(out, x)
}
}
return out
}
// Drop takes a slice of type []T and a function of type func(T) bool. It returns a newly
// allocated slice containing only those elements of the input slice that do
// not satisfy the function, that is, it removes elements that satisfy the
// function.
func Drop[T any](slice []T, function func(T) bool) []T {
out := make([]T, 0)
for _, x := range slice {
if !function(x) {
out = append(out, x)
}
}
return out
}
// ChooseInPlace is like Choose, but overwrites the slice rather than returning
// a newly allocated slice. Since ChooseInPlace must modify the slice,
// it takes as argument a pointer to a slice rather than a slice.
func ChooseInPlace[T any](pointerToSlice *[]T, function func(T) bool) {
out := (*pointerToSlice)[:0]
for _, x := range *pointerToSlice {
if function(x) {
out = append(out, x)
}
}
*pointerToSlice = out
}
// DropInPlace is like Drop, but overwrites the slice rather than returning a
// newly allocated slice. Since DropInPlace must modify the slice,
// it takes as argument a pointer to a slice rather than a slice.
func DropInPlace[T any](pointerToSlice *[]T, function func(T) bool) {
out := (*pointerToSlice)[:0]
for _, x := range *pointerToSlice {
if !function(x) {
out = append(out, x)
}
}
*pointerToSlice = out
}