|
8 | 8 |
|
9 | 9 | "github.com/BooleanCat/go-functional/v2/internal/assert" |
10 | 10 | "github.com/BooleanCat/go-functional/v2/it" |
| 11 | + "github.com/BooleanCat/go-functional/v2/it/filter" |
11 | 12 | ) |
12 | 13 |
|
13 | 14 | func ExampleTake() { |
@@ -102,3 +103,86 @@ func TestTake2YieldFalse(t *testing.T) { |
102 | 103 | return false |
103 | 104 | }) |
104 | 105 | } |
| 106 | + |
| 107 | +func ExampleTakeWhile() { |
| 108 | + for number := range it.TakeWhile(slices.Values([]int{1, 2, 3, 4, 5}), filter.LessThan(4)) { |
| 109 | + fmt.Println(number) |
| 110 | + } |
| 111 | + |
| 112 | + // Output: |
| 113 | + // 1 |
| 114 | + // 2 |
| 115 | + // 3 |
| 116 | +} |
| 117 | + |
| 118 | +func TestTakeWhileYieldsFalse(t *testing.T) { |
| 119 | + t.Parallel() |
| 120 | + |
| 121 | + seq := it.TakeWhile(slices.Values([]int{1, 2, 3, 4, 5}), filter.LessThan(4)) |
| 122 | + |
| 123 | + seq(func(n int) bool { |
| 124 | + return false |
| 125 | + }) |
| 126 | +} |
| 127 | + |
| 128 | +func TestTakeWhileEmpty(t *testing.T) { |
| 129 | + t.Parallel() |
| 130 | + |
| 131 | + assert.Empty[int](t, slices.Collect(it.TakeWhile(it.Exhausted[int](), filter.Passthrough))) |
| 132 | +} |
| 133 | + |
| 134 | +func TestTakeWhileNeverTake(t *testing.T) { |
| 135 | + t.Parallel() |
| 136 | + |
| 137 | + numbers := slices.Collect(it.TakeWhile(slices.Values([]int{1, 2, 3}), func(int) bool { return false })) |
| 138 | + assert.Empty[int](t, numbers) |
| 139 | +} |
| 140 | + |
| 141 | +func TestTakeWhileTakeAll(t *testing.T) { |
| 142 | + t.Parallel() |
| 143 | + |
| 144 | + numbers := slices.Collect(it.TakeWhile(slices.Values([]int{1, 2, 3}), filter.Passthrough)) |
| 145 | + assert.SliceEqual(t, []int{1, 2, 3}, numbers) |
| 146 | +} |
| 147 | + |
| 148 | +func ExampleTakeWhile2() { |
| 149 | + _, values := it.Collect2(it.TakeWhile2(slices.All([]int{1, 2, 3}), func(i int, v int) bool { |
| 150 | + return v < 3 |
| 151 | + })) |
| 152 | + |
| 153 | + fmt.Println(values) |
| 154 | + // Output: [1 2] |
| 155 | +} |
| 156 | + |
| 157 | +func TestTakeWhile2YieldsFalse(t *testing.T) { |
| 158 | + t.Parallel() |
| 159 | + |
| 160 | + seq := it.TakeWhile2(slices.All([]int{1, 2, 3}), func(i int, v int) bool { |
| 161 | + return v < 3 |
| 162 | + }) |
| 163 | + |
| 164 | + seq(func(i int, v int) bool { |
| 165 | + return false |
| 166 | + }) |
| 167 | +} |
| 168 | + |
| 169 | +func TestTakeWhile2Empty(t *testing.T) { |
| 170 | + t.Parallel() |
| 171 | + |
| 172 | + _, values := it.Collect2(it.TakeWhile2(it.Exhausted2[int, int](), filter.Passthrough2)) |
| 173 | + assert.Empty[int](t, values) |
| 174 | +} |
| 175 | + |
| 176 | +func TestTakeWhile2NeverTake(t *testing.T) { |
| 177 | + t.Parallel() |
| 178 | + |
| 179 | + _, values := it.Collect2(it.TakeWhile2(slices.All([]int{1, 2, 3}), func(int, int) bool { return false })) |
| 180 | + assert.Empty[int](t, values) |
| 181 | +} |
| 182 | + |
| 183 | +func TestTakeWhile2TakeAll(t *testing.T) { |
| 184 | + t.Parallel() |
| 185 | + |
| 186 | + _, values := it.Collect2(it.TakeWhile2(slices.All([]int{1, 2, 3}), filter.Passthrough2)) |
| 187 | + assert.SliceEqual(t, []int{1, 2, 3}, values) |
| 188 | +} |
0 commit comments