Skip to content
Open
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module go.bug.st/f

go 1.22.3
go 1.25

require github.com/stretchr/testify v1.9.0

Expand Down
41 changes: 41 additions & 0 deletions iter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package f
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing license header


import "iter"

// FilterIter takes an iterator and a matcher and returns only those elements that satisfy the matcher.
Copy link
Member

@cmaglie cmaglie Jan 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// FilterIter takes an iterator and a matcher and returns only those elements that satisfy the matcher.
// FilterIter takes an iterator and a matcher and returns an iterator that yields only those
// elements that satisfy the matcher.

func FilterIter[T any](values iter.Seq[T], matcher Matcher[T]) iter.Seq[T] {
return func(yield func(x T) bool) {
for x := range values {
if matcher(x) {
if !yield(x) {
return
}
}
}
}
}

// Map applies the Mapper function to each element of the iterator.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Map applies the Mapper function to each element of the iterator.
// MapIter applies the Mapper function to each element of the iterator.

func MapIter[T, U any](values iter.Seq[T], mapper Mapper[T, U]) iter.Seq[U] {
return func(yield func(x U) bool) {
for x := range values {
if !yield(mapper(x)) {
return
}
}
}
}

// Reducer is a function that reduces an iterator's elements to a single value.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Reducer is a function that reduces an iterator's elements to a single value.
// ReduceIter is a function that reduces an iterator's elements to a single value.

func ReduceIter[T any](values iter.Seq[T], reducer Reducer[T], initialValue ...T) T {
var result T
if len(initialValue) > 1 {
panic("initialValue must be a single value")
} else if len(initialValue) == 1 {
result = initialValue[0]
}
for v := range values {
result = reducer(result, v)
}
return result
}
117 changes: 117 additions & 0 deletions iter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package f

import (
"slices"
"testing"

"github.com/stretchr/testify/assert"
)

func TestFilterIter(t *testing.T) {
tests := []struct {
name string
input []int
matcher Matcher[int]
expected []int
}{
{
name: "even numbers",
input: []int{1, 2, 3, 4, 5},
matcher: func(x int) bool { return x%2 == 0 },
expected: []int{2, 4},
},
{
name: "odd numbers",
input: []int{1, 2, 3, 4, 5},
matcher: func(x int) bool { return x%2 == 1 },
expected: []int{1, 3, 5},
},
{
name: "none",
input: []int{2, 4, 6},
matcher: func(x int) bool { return x < 0 },
expected: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
values := slices.Values(tt.input)
result := slices.Collect(FilterIter(values, tt.matcher))
assert.Equal(t, tt.expected, result)
})
}
}

func TestMapIter(t *testing.T) {
tests := []struct {
name string
input []int
mapper Mapper[int, int]
expected []int
}{
{
name: "double",
input: []int{1, 2, 3},
mapper: func(x int) int { return x * 2 },
expected: []int{2, 4, 6},
},
{
name: "negate",
input: []int{1, -2, 3},
mapper: func(x int) int { return -x },
expected: []int{-1, 2, -3},
},
{
name: "identity",
input: []int{1, 2, 3},
mapper: func(x int) int { return x },
expected: []int{1, 2, 3},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
values := slices.Values(tt.input)
result := slices.Collect(MapIter(values, tt.mapper))
assert.Equal(t, tt.expected, result)
})
}
}

func TestReduceIter(t *testing.T) {
tests := []struct {
name string
input []int
reducer Reducer[int]
initialValue int
expected int
}{
{
name: "sum",
input: []int{1, 2, 3, 4},
reducer: func(a, b int) int { return a + b },
initialValue: 0,
expected: 10,
},
{
name: "product",
input: []int{1, 2, 3, 4},
reducer: func(a, b int) int { return a * b },
initialValue: 1,
expected: 24,
},
{
name: "subtract",
input: []int{10, 2, 3},
reducer: func(a, b int) int { return a - b },
initialValue: 20,
expected: 5,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
values := slices.Values(tt.input)
result := ReduceIter(values, tt.reducer, tt.initialValue)
assert.Equal(t, tt.expected, result)
})
}
}
14 changes: 14 additions & 0 deletions slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package f

import (
"iter"
"runtime"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -124,3 +125,16 @@ func Count[T any](in []T, matcher Matcher[T]) int {
}
return count
}

// RefIter takes a slice of type []T and returns an iterator that yields
// pointers to each element of the slice.
func RefIter[T any](slice []T) iter.Seq[*T] {
return func(yield func(*T) bool) {
for i := range slice {
if !yield(&slice[i]) {
return
}
}
}
}

34 changes: 34 additions & 0 deletions slices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
package f_test

import (
"slices"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
f "go.bug.st/f"
)
Expand Down Expand Up @@ -100,3 +102,35 @@ func TestCount(t *testing.T) {
require.Equal(t, 0, f.Count(a, f.Equals("ddd")))
require.Equal(t, 3, f.Count(a, f.NotEquals("ddd")))
}

func TestRefIter(t *testing.T) {
type foo struct {
value int
}
values := []foo{
{value: 1},
{value: 2},
{value: 3},
}

t.Run("not working for range", func(t *testing.T) {
for _, v := range values {
v.value *= 10
}
assert.Equal(t, []foo{{1}, {2}, {3}}, values)
})

t.Run("not working slices.Values", func(t *testing.T) {
for v := range slices.Values(values) {
v.value *= 10
}
assert.Equal(t, []foo{{1}, {2}, {3}}, values)
})

t.Run("working RefIter", func(t *testing.T) {
for v := range f.RefIter(values) {
v.value *= 10
}
assert.Equal(t, []foo{{10}, {20}, {30}}, values)
})
}