-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
157 lines (136 loc) · 2.73 KB
/
main_test.go
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"fmt"
"testing"
)
type Iter[I any] func(func(yield I) bool)
type Product struct {
name string
price int
description string
}
func main() {
products := []Product{{
name: "perfume",
price: 40,
description: "paco rabanne collection",
},
{
name: "cigarettes",
price: 15,
description: "peruvian tabacco",
},
{
name: "shoes",
price: 30,
description: "comfortable trainers",
},
}
fmt.Println("Product Filter iteration")
priceRange := func(p Product) bool {
return p.price == 30
}
for product := range Filter(MakeIterable(products), priceRange) {
fmt.Println(product)
}
fmt.Println("Product Map iteration")
mapProduct := func(p Product) Product {
if p.name == "perfume" {
p.price = p.price + 10
return p
}
return p
}
for product := range Map(MakeIterable(products), mapProduct) {
fmt.Println(product)
}
}
func MakeIterable[S ~[]I, I any](s S) Iter[I] {
return func(yield func(I) bool) {
for _, item := range s {
yield(item)
}
}
}
func Filter[I any](iter Iter[I], check func(I) bool) Iter[I] {
return func(yield func(I) bool) {
iter(func(item I) bool {
if check(item) {
yield(item)
}
return true
})
}
}
func Map[I any, T any](iter Iter[I], mapf func(I) T) Iter[T] {
return func(yield func(T) bool) {
iter(func(item I) bool {
return yield(mapf(item))
})
}
}
// 139254 8790 ns/op
func BenchmarkFilterIterator(b *testing.B) {
products := []Product{
{
name: "perfume",
price: 40,
description: "feminine perfume",
},
{
name: "cigarettes",
price: 15,
description: "peruvian tobacco",
},
{
name: "shoes",
price: 30,
description: "indian kussa",
},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
fmt.Println("Product Filter iteration")
priceRange := func(p Product) bool {
return p.price == 30
}
for product := range Filter(MakeIterable(products), priceRange) {
fmt.Println(product)
}
}
}
// 272203 4884 ns/op
func BenchmarkFilter(b *testing.B) {
products := []Product{
{
name: "perfume",
price: 40,
description: "feminine perfume",
},
{
name: "cigarettes",
price: 15,
description: "peruvian tobacco",
},
{
name: "shoes",
price: 30,
description: "indian kussa",
},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
filteredProducts := []Product{}
priceRange := func(p Product) bool {
return p.price == 30
}
for _, product := range products {
if priceRange(product) {
filteredProducts = append(filteredProducts, product)
}
}
for _, product := range filteredProducts {
fmt.Println(product)
}
}
}