-
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.
Add Copies iterator returning copies of a specified object
- Loading branch information
Showing
3 changed files
with
82 additions
and
0 deletions.
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,37 @@ | ||
// Copyright (c) 2022 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 valit | ||
|
||
import ( | ||
"github.com/0x5a17ed/itkit" | ||
) | ||
|
||
// Copier represents a struct that can copy itself. | ||
type Copier[T any] interface { | ||
Copy() T | ||
} | ||
|
||
// CopyIterator represents an iterator yielding copies of a given | ||
// value on every iteration of the iterator. | ||
type CopyIterator[T Copier[T]] struct{ src, cur T } | ||
|
||
func (it CopyIterator[T]) Value() T { return it.cur } | ||
func (it CopyIterator[T]) Next() bool { it.cur = it.src.Copy(); return true } | ||
|
||
// Copies provides an iterator which yields copies of a given value on | ||
// every iteration of the iterator. | ||
func Copies[T Copier[T]](v T) itkit.Iterator[T] { | ||
return &CopyIterator[T]{src: v} | ||
} |
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,44 @@ | ||
// Copyright (c) 2022 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 valit_test | ||
|
||
import ( | ||
"testing" | ||
|
||
assertPkg "github.com/stretchr/testify/assert" | ||
requirePkg "github.com/stretchr/testify/require" | ||
|
||
"github.com/0x5a17ed/itkit/iters/valit" | ||
) | ||
|
||
type object struct{ copies int } | ||
|
||
func (t *object) Copy() *object { | ||
t.copies++ | ||
return &object{} | ||
} | ||
|
||
func TestCopies(t *testing.T) { | ||
var o object | ||
iter := valit.Copies(&o) | ||
|
||
for i := 0; i < 10; i++ { | ||
requirePkg.True(t, iter.Next()) | ||
assertPkg.NotSame(t, &o, iter.Value()) | ||
assertPkg.Same(t, iter.Value(), iter.Value()) | ||
} | ||
|
||
assertPkg.Equal(t, 10, o.copies) | ||
} |
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