Skip to content

Commit

Permalink
add Drop function to drop n items
Browse files Browse the repository at this point in the history
  • Loading branch information
0x5a17ed committed May 24, 2024
1 parent ac82baf commit c398ec1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
10 changes: 10 additions & 0 deletions itlib/each.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,13 @@ func Sum[T constraints.Ordered](it itkit.Iterator[T]) T {
var zero T
return SumWithInitial(zero, it)
}

// Drop drops n items from the iterator.
func Drop[T any](n uint, it itkit.Iterator[T]) itkit.Iterator[T] {
for i := uint(0); i < n; i++ {
if !it.Next() {
break
}
}
return it
}
32 changes: 32 additions & 0 deletions itlib/each_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,35 @@ func TestSum(t *testing.T) {
assertpkg.Equal(t, "abc", n)
})
}

func TestDrop(t *testing.T) {
t.Run("empty", func(t *testing.T) {
it := itlib.Empty[any]()

it2 := itlib.Drop(4, it)
assertpkg.Same(t, it, it2)
})

t.Run("exactly", func(t *testing.T) {
assert := assertpkg.New(t)

it := rangeit.Range(5)

it2 := itlib.Drop(5, it)
assert.Same(it, it2)

assert.False(it.Next())
})

t.Run("less", func(t *testing.T) {
assert := assertpkg.New(t)

it := rangeit.Range(5)

it2 := itlib.Drop(4, it)
assert.Same(it, it2)

assert.True(it.Next())
assert.Equal(it.Value(), 4)
})
}

0 comments on commit c398ec1

Please sign in to comment.