Skip to content

Commit b3ea617

Browse files
committed
Support Stringer for MapIter
1 parent f278681 commit b3ea617

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

iter/map.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package iter
22

3-
import "github.com/BooleanCat/go-functional/option"
3+
import (
4+
"fmt"
5+
"reflect"
6+
7+
"github.com/BooleanCat/go-functional/option"
8+
)
49

510
// MapIter iterator, see [Map].
611
type MapIter[T, U any] struct {
@@ -47,4 +52,13 @@ func (iter *MapIter[T, U]) Next() option.Option[U] {
4752
return option.Some(iter.fun(value))
4853
}
4954

50-
var _ Iterator[struct{}] = new(MapIter[struct{}, struct{}])
55+
// String implements the [fmt.Stringer] interface
56+
func (iter MapIter[T, U]) String() string {
57+
var instanceOfU U
58+
return fmt.Sprintf("Iterator<Map, type=%s>", reflect.TypeOf(instanceOfU))
59+
}
60+
61+
var (
62+
_ fmt.Stringer = new(MapIter[struct{}, struct{}])
63+
_ Iterator[struct{}] = new(MapIter[struct{}, struct{}])
64+
)

iter/map_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package iter_test
22

33
import (
44
"fmt"
5+
"strconv"
56
"testing"
67

78
"github.com/BooleanCat/go-functional/internal/assert"
@@ -26,6 +27,11 @@ func ExampleTransform() {
2627
// Output: [2 3 4]
2728
}
2829

30+
func ExampleMapIter_String() {
31+
fmt.Println(iter.Map[int, string](iter.Count(), strconv.Itoa))
32+
// Output: Iterator<Map, type=string>
33+
}
34+
2935
func ExampleTransform_method() {
3036
addTwo := func(number int) int { return number + 2 }
3137
numbers := iter.Count().Transform(addTwo).Take(3).Collect()
@@ -61,3 +67,11 @@ func TestTransform(t *testing.T) {
6167

6268
assert.SliceEqual[int](t, numbers, []int{2, 3, 4})
6369
}
70+
71+
func TestMapIter_String(t *testing.T) {
72+
assert.Equal(
73+
t,
74+
iter.Map[int, string](iter.Count(), strconv.Itoa).String(),
75+
"Iterator<Map, type=string>",
76+
)
77+
}

0 commit comments

Comments
 (0)