From 574f36e64e120163e155e1d38f548473eff5daa5 Mon Sep 17 00:00:00 2001 From: amorozov Date: Tue, 25 Jun 2024 13:36:47 +0300 Subject: [PATCH] feature: added filter method to array list --- .../array_list/array_list_methods.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/pkg/collections/array_list/array_list_methods.go b/pkg/collections/array_list/array_list_methods.go index b5f0c96..3146805 100644 --- a/pkg/collections/array_list/array_list_methods.go +++ b/pkg/collections/array_list/array_list_methods.go @@ -208,3 +208,21 @@ func (self *ArrayList[T]) Find(filter func(T) bool) *optional.Optional[T] { return optional.Empty[T]() } + +func (self *ArrayList[T]) Filter(filter func(T) bool) *ArrayList[T] { + return self.FilterTo(filter, NewArrayList[T]()) +} + +func (self *ArrayList[T]) FilterTo(filter func(T) bool, resultList *ArrayList[T]) *ArrayList[T] { + if resultList == nil { + panic(fmt.Errorf("can not filter %v to nil list", self)) + } + + self.ForEach(func(value T) { + if filter(value) { + resultList.Add(value) + } + }) + + return resultList +}