Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Compact iterator #147

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,19 @@ for number := range itx.FromChannel(items).Exclude(filter.IsZero) {
> In order to prevent a deadlock, the channel must be closed before attemping to stop the iterator
> when it's used in a pull style. See [iter.Pull](https://pkg.go.dev/iter#Pull).

### Compact

Compact yields all values from a delegate iterator that are not zero values. It is functionally
equivalent to `it.Filter(delegate, filter.IsZero)`.

```go
words := it.Compact(slices.Values([]string{"foo", "", "bar", "", ""}))
```

<!-- prettier-ignore -->
> [!NOTE]
> The `itx` package does not contain `Compact` due to limitations with Go's type system.

### Cycle

Cycle yields all values from an iterator before returning to the beginning and yielding all values
Expand Down
19 changes: 19 additions & 0 deletions it/compact.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package it

import "iter"

// Compact yields all values from a delegate iterator that are not zero values.
func Compact[V comparable](delegate func(func(V) bool)) iter.Seq[V] {
return func(yield func(V) bool) {
for value := range delegate {
var zero V
if zero == value {
continue
}

if !yield(value) {
return
}
}
}
}
47 changes: 47 additions & 0 deletions it/compact_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package it_test

import (
"fmt"
"slices"
"testing"

"github.com/BooleanCat/go-functional/v2/internal/assert"
"github.com/BooleanCat/go-functional/v2/it"
)

func ExampleCompact() {
words := slices.Values([]string{"", "foo", "", "", "bar", ""})
fmt.Println(slices.Collect(it.Compact(words)))
// Output: [foo bar]
}

func TestCompactEmpty(t *testing.T) {
t.Parallel()

words := slices.Collect(it.Compact(it.Exhausted[string]()))
assert.Empty[string](t, words)
}

func TestCompactOnlyEmpty(t *testing.T) {
t.Parallel()

words := slices.Values([]string{"", "", "", ""})
assert.Empty[string](t, slices.Collect(it.Compact(words)))
}

func TestCompactOnlyNotEmpty(t *testing.T) {
t.Parallel()

words := slices.Values([]string{"foo", "bar"})
assert.SliceEqual(t, slices.Collect(it.Compact(words)), []string{"foo", "bar"})
}

func TestCompactYieldFalse(t *testing.T) {
t.Parallel()

words := it.Compact(slices.Values([]string{"foo", "bar"}))

words(func(string) bool {
return false
})
}