From 6ff8b920f98baebda271df6fa2e974cca6875089 Mon Sep 17 00:00:00 2001 From: x5a17ed <0x5a17ed@tuta.io> Date: Sun, 6 Nov 2022 18:38:34 +0100 Subject: [PATCH] add new Fill iterator --- iters/valit/docs.go | 20 +++++++++++++++++ iters/valit/fill.go | 30 +++++++++++++++++++++++++ iters/valit/fill_test.go | 48 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 iters/valit/docs.go create mode 100644 iters/valit/fill.go create mode 100644 iters/valit/fill_test.go diff --git a/iters/valit/docs.go b/iters/valit/docs.go new file mode 100644 index 0000000..fb267ee --- /dev/null +++ b/iters/valit/docs.go @@ -0,0 +1,20 @@ +// 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 allows for native Go values to be used with +// iterators. +// +// Iterators: +// - Fill - yields the same value forever. +package valit diff --git a/iters/valit/fill.go b/iters/valit/fill.go new file mode 100644 index 0000000..3209a04 --- /dev/null +++ b/iters/valit/fill.go @@ -0,0 +1,30 @@ +// 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" +) + +// FillIterator represents an iterator yielding the same value over and over again. +type FillIterator[T any] struct{ v T } + +func (it FillIterator[T]) Value() T { return it.v } +func (it FillIterator[T]) Next() bool { return true } + +// Fill provides an iterator which yields the same value over and over again. +func Fill[T any](v T) itkit.Iterator[T] { + return &FillIterator[T]{v: v} +} diff --git a/iters/valit/fill_test.go b/iters/valit/fill_test.go new file mode 100644 index 0000000..b2dd3bd --- /dev/null +++ b/iters/valit/fill_test.go @@ -0,0 +1,48 @@ +// 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" +) + +func TestFillIterator(t *testing.T) { + type object struct{} + + t.Run("integer", func(t *testing.T) { + iter := valit.Fill(10) + + var got []int + for i := 0; i < 5; i += 1 { + requirePkg.True(t, iter.Next()) + got = append(got, iter.Value()) + } + assertPkg.Equal(t, []int{10, 10, 10, 10, 10}, got) + }) + + t.Run("struct", func(t *testing.T) { + var o object + iter := valit.Fill(&o) + for i := 0; i < 5; i += 1 { + requirePkg.True(t, iter.Next()) + assertPkg.Same(t, &o, iter.Value()) + } + }) +}