From 591fc9020362d5f3805e936090fd068b474d0ffa Mon Sep 17 00:00:00 2001 From: x5a17ed <0x5a17ed@tuta.io> Date: Sun, 6 Nov 2022 18:44:59 +0100 Subject: [PATCH] Add Copies iterator returning copies of a specified object --- iters/valit/copies.go | 37 ++++++++++++++++++++++++++++++++ iters/valit/copies_test.go | 44 ++++++++++++++++++++++++++++++++++++++ iters/valit/docs.go | 1 + 3 files changed, 82 insertions(+) create mode 100644 iters/valit/copies.go create mode 100644 iters/valit/copies_test.go diff --git a/iters/valit/copies.go b/iters/valit/copies.go new file mode 100644 index 0000000..4da6d6e --- /dev/null +++ b/iters/valit/copies.go @@ -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 +// +// +// +// 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} +} diff --git a/iters/valit/copies_test.go b/iters/valit/copies_test.go new file mode 100644 index 0000000..35cfbf9 --- /dev/null +++ b/iters/valit/copies_test.go @@ -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 +// +// +// +// 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) +} diff --git a/iters/valit/docs.go b/iters/valit/docs.go index fb267ee..a71c4c7 100644 --- a/iters/valit/docs.go +++ b/iters/valit/docs.go @@ -16,5 +16,6 @@ // iterators. // // Iterators: +// - Copies - yields copies of a given value forever. // - Fill - yields the same value forever. package valit