Skip to content

Commit

Permalink
Add Copies iterator returning copies of a specified object
Browse files Browse the repository at this point in the history
  • Loading branch information
0x5a17ed committed Nov 6, 2022
1 parent 6ff8b92 commit 591fc90
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
37 changes: 37 additions & 0 deletions iters/valit/copies.go
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}
}
44 changes: 44 additions & 0 deletions iters/valit/copies_test.go
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)
}
1 change: 1 addition & 0 deletions iters/valit/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
// iterators.
//
// Iterators:
// - Copies - yields copies of a given value forever.
// - Fill - yields the same value forever.
package valit

0 comments on commit 591fc90

Please sign in to comment.