Skip to content

Commit

Permalink
add new Fill iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
0x5a17ed committed Nov 6, 2022
1 parent 61d6212 commit 6ff8b92
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
20 changes: 20 additions & 0 deletions iters/valit/docs.go
Original file line number Diff line number Diff line change
@@ -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
//
// <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 allows for native Go values to be used with
// iterators.
//
// Iterators:
// - Fill - yields the same value forever.
package valit
30 changes: 30 additions & 0 deletions iters/valit/fill.go
Original file line number Diff line number Diff line change
@@ -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
//
// <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"
)

// 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}
}
48 changes: 48 additions & 0 deletions iters/valit/fill_test.go
Original file line number Diff line number Diff line change
@@ -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
//
// <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"
)

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())
}
})
}

0 comments on commit 6ff8b92

Please sign in to comment.