Skip to content

Commit

Permalink
flatMap to int and string
Browse files Browse the repository at this point in the history
  • Loading branch information
Javier Orfo committed Nov 20, 2024
1 parent cff67de commit e6d733a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ type Steam[T any] interface {
MapToString(mapper func(T) string) Steam[string]
FilterMapToAny(predicate func(T) bool, mapper func(T) any) Steam[any]
FlatMapToAny(mapper func(T) Steam[any]) Steam[any]
FlatMapToInt(mapper func(T) Steam[int]) Steam[int]
FlatMapToString(mapper func(T) Steam[string]) Steam[string]
ForEach(consumer func(T))
Peek(consumer func(T)) Steam[T]
Limit(limit int) Steam[T]
Expand Down
20 changes: 20 additions & 0 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,26 @@ func (list List[T]) FlatMapToAny(mapper func(T) Steam[any]) Steam[any] {
return results
}

// FlatMapToInt applies the provided mapper function to each element in the List, which returns a Steam,
// and concatenates the results into a single List of type int.
func (list List[T]) FlatMapToInt(mapper func(T) Steam[int]) Steam[int] {
results := make(List[int], 0, list.Count())
for _, v := range list {
results = slices.Concat(results, mapper(v).(List[int]))
}
return results
}

// FlatMapToString applies the provided mapper function to each element in the List, which returns a Steam,
// and concatenates the results into a single List of type string.
func (list List[T]) FlatMapToString(mapper func(T) Steam[string]) Steam[string] {
results := make(List[string], 0, list.Count())
for _, v := range list {
results = slices.Concat(results, mapper(v).(List[string]))
}
return results
}

// Limit restricts the number of elements in the List to the specified limit and returns a new List.
func (list List[T]) Limit(limit int) Steam[T] {
if limit > list.Count() {
Expand Down
24 changes: 24 additions & 0 deletions list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,30 @@ func TestFlatMapToAny(t *testing.T) {
assert.Equal(t, List[any]{"v1", "v2", "v2", "v4", "v3", "v6"}, flattened)
}

func TestFlatMapToInt(t *testing.T) {
list := List[List[int]]{{1, 2}, {2, 4}, {3, 6}}
flattened := list.FlatMapToInt(func(s List[int]) Steam[int] {
results := make(List[int], s.Count())
for i, v := range s.Collect() {
results[i] = v
}
return results
})
assert.Equal(t, List[int]{1, 2, 2, 4, 3, 6}, flattened)
}

func TestFlatMapToString(t *testing.T) {
list := List[List[int]]{{1, 2}, {2, 4}, {3, 6}}
flattened := list.FlatMapToString(func(s List[int]) Steam[string] {
results := make(List[string], s.Count())
for i, v := range s.Collect() {
results[i] = fmt.Sprintf("v%v", v)
}
return results
})
assert.Equal(t, List[string]{"v1", "v2", "v2", "v4", "v3", "v6"}, flattened)
}

func TestLimit(t *testing.T) {
limited := Of(1, 2, 3, 4, 5).Limit(3)
assert.Equal(t, List[int]{1, 2, 3}, limited)
Expand Down
8 changes: 8 additions & 0 deletions steam.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ type Steam[T any] interface {
// the provided mapper function and flattens the result into a single Steam.
FlatMapToAny(mapper func(T) Steam[any]) Steam[any]

// FlatMapToInt transforms each element of the Steam into a new Steam using
// the provided mapper function and flattens the result into a single Steam.
FlatMapToInt(mapper func(T) Steam[int]) Steam[int]

// FlatMapToString transforms each element of the Steam into a new Steam using
// the provided mapper function and flattens the result into a single Steam.
FlatMapToString(mapper func(T) Steam[string]) Steam[string]

// ForEach executes the provided consumer function for each element in the Steam.
ForEach(consumer func(T))

Expand Down

0 comments on commit e6d733a

Please sign in to comment.