From cff67de98c3de2ec7d88ae60ab296ba3982906f8 Mon Sep 17 00:00:00 2001 From: Javier Orfo Date: Fri, 8 Nov 2024 12:25:02 -0300 Subject: [PATCH] limit fixed --- list.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/list.go b/list.go index d5ecdd1..e6d985c 100644 --- a/list.go +++ b/list.go @@ -79,10 +79,11 @@ func (list List[T]) FlatMapToAny(mapper func(T) Steam[any]) Steam[any] { // 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] { - results := make(List[T], 0) - for i := 0; i < len(list) && i < limit; i++ { - results = append(results, list[i]) + if limit > list.Count() { + limit = list.Count() } + results := make(List[T], limit) + copy(results, list[:limit]) return results }