-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
160 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright (c) 2024 individual contributors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// <https://www.apache.org/licenses/LICENSE-2.0> | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package itlib | ||
|
||
import ( | ||
"github.com/0x5a17ed/itkit" | ||
) | ||
|
||
// ChunkIterator yields iterators yielding up to n items from a source | ||
// iterator until the source iterator is exhausted. | ||
type ChunkIterator[T any] struct { | ||
src *PeekIterator[T] | ||
|
||
cur *LimitIterator[T] | ||
n uint | ||
} | ||
|
||
// Ensure ChunkIterator implements the iterator interface. | ||
var _ itkit.Iterator[itkit.Iterator[struct{}]] = &ChunkIterator[struct{}]{} | ||
|
||
// Next implements the [itkit.Iterator.Next] interface. | ||
func (it *ChunkIterator[T]) Next() bool { | ||
if it.cur != nil && it.cur.n > 0 { | ||
// Drop any items not consumed in the previous chunk | ||
// to make sure that the next chunk only contains | ||
// new items. | ||
Drop(it.cur.n, it.src.Iter()) | ||
} | ||
|
||
if _, ok := it.src.Peek(); !ok { | ||
return false | ||
} | ||
it.cur = newLimitIterator(it.n, it.src.Iter()) | ||
return true | ||
} | ||
|
||
// Value implements the [itkit.Iterator.Value] interface. | ||
func (it *ChunkIterator[T]) Value() itkit.Iterator[T] { | ||
return it.cur | ||
} | ||
|
||
// Chunk returns a new [ChunkIterator] value. | ||
func Chunk[T any](n uint, src itkit.Iterator[T]) itkit.Iterator[itkit.Iterator[T]] { | ||
return &ChunkIterator[T]{src: newPeekIterator(src), n: n} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Copyright (c) 2024 individual contributors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// <https://www.apache.org/licenses/LICENSE-2.0> | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package itlib_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/0x5a17ed/itkit" | ||
"github.com/0x5a17ed/itkit/iters/sliceit" | ||
"github.com/0x5a17ed/itkit/itlib" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestChunk(t *testing.T) { | ||
tt := []struct { | ||
name string | ||
inp []string | ||
tx func(el itkit.Iterator[string]) itkit.Iterator[string] | ||
exp [][]string | ||
}{ | ||
{ | ||
name: "emtpy", | ||
inp: nil, | ||
exp: nil, | ||
}, | ||
|
||
{ | ||
name: "partial-first", | ||
inp: []string{"A", "B"}, | ||
exp: [][]string{{"A", "B"}}, | ||
}, | ||
|
||
{ | ||
name: "complete", | ||
inp: []string{ | ||
"A", "B", "C", "D", "E", "F", | ||
}, | ||
exp: [][]string{ | ||
{"A", "B", "C"}, | ||
{"D", "E", "F"}, | ||
}, | ||
}, | ||
|
||
{ | ||
name: "partial-last", | ||
inp: []string{ | ||
"A", "B", "C", "D", "E", "F", "G", | ||
}, | ||
exp: [][]string{ | ||
{"A", "B", "C"}, | ||
{"D", "E", "F"}, | ||
{"G"}, | ||
}, | ||
}, | ||
|
||
{ | ||
name: "half-consumed", | ||
inp: []string{ | ||
"A", "B", "C", "D", "E", "F", "G", | ||
}, | ||
tx: func(el itkit.Iterator[string]) itkit.Iterator[string] { | ||
return itlib.Limit(el, 2) | ||
}, | ||
exp: [][]string{ | ||
{"A", "B"}, | ||
{"D", "E"}, | ||
{"G"}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range tt { | ||
tc := tc | ||
t.Run(tc.name, func(t *testing.T) { | ||
it := itlib.Chunk(3, sliceit.In(tc.inp)) | ||
|
||
if tc.tx != nil { | ||
it = itlib.Map(it, tc.tx) | ||
} | ||
|
||
got := sliceit.To(itlib.Map(it, sliceit.To[string])) | ||
|
||
assert.Equal(t, tc.exp, got) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters