From e6d733a334c089c067329159b30413e6769ec3d0 Mon Sep 17 00:00:00 2001 From: Javier Orfo Date: Wed, 20 Nov 2024 18:05:27 -0300 Subject: [PATCH] flatMap to int and string --- README.md | 2 ++ list.go | 20 ++++++++++++++++++++ list_test.go | 24 ++++++++++++++++++++++++ steam.go | 8 ++++++++ 4 files changed, 54 insertions(+) diff --git a/README.md b/README.md index 4796c35..0deaf99 100644 --- a/README.md +++ b/README.md @@ -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] diff --git a/list.go b/list.go index e6d985c..52afd92 100644 --- a/list.go +++ b/list.go @@ -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() { diff --git a/list_test.go b/list_test.go index 151f5de..e2596d9 100644 --- a/list_test.go +++ b/list_test.go @@ -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) diff --git a/steam.go b/steam.go index a924b7d..ffd2efb 100644 --- a/steam.go +++ b/steam.go @@ -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))