From 29af2702ee93f0a6f005bc8c8294202dcb281eda Mon Sep 17 00:00:00 2001 From: x5a17ed <0x5a17ed@tuta.io> Date: Fri, 24 May 2024 15:55:47 +0200 Subject: [PATCH] add Any function --- itlib/each.go | 6 ++++++ itlib/each_test.go | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/itlib/each.go b/itlib/each.go index cbeaf50..9851b54 100644 --- a/itlib/each.go +++ b/itlib/each.go @@ -122,3 +122,9 @@ func Drop[T any](n uint, it itkit.Iterator[T]) itkit.Iterator[T] { } return it } + +// Any tests if any item in the iterator matches a predicate. +func Any[T any](it itkit.Iterator[T], fn EachFn[T]) (ok bool) { + Each(it, func(x T) bool { ok = fn(x); return ok }) + return +} diff --git a/itlib/each_test.go b/itlib/each_test.go index cb5d7e8..5ad470f 100644 --- a/itlib/each_test.go +++ b/itlib/each_test.go @@ -204,3 +204,23 @@ func TestDrop(t *testing.T) { assert.Equal(it.Value(), 4) }) } + +func TestAny(t *testing.T) { + type testCase[T any] struct { + name string + args []bool + wantOk bool + } + tt := []testCase[bool]{ + {"false", []bool{false, false, false}, false}, + {"true", []bool{false, false, true}, true}, + } + for _, tt := range tt { + t.Run(tt.name, func(t *testing.T) { + got := itlib.Any(sliceit.In(tt.args), func(item bool) bool { + return item + }) + assertpkg.Equal(t, tt.wantOk, got) + }) + } +}