slice

package
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 4, 2024 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package slice implements an iterator that traverses uni-directionally over a generic slice of elements

Slice supports the SizeHint interface.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Iterator

type Iterator[T any] struct {
	// contains filtered or unexported fields
}

Iterator traverses over a slice of element of type T.

Example
package main

import (
	"context"
	"fmt"

	"github.com/jake-scott/go-functional/iter/slice"
)

func main() {
	input := []string{"dog", "cat", "fox", "pigeon"}

	ctx := context.Background()
	iter := slice.New(input)

	for iter.Next(ctx) {
		fmt.Printf("Animal: <%s>\n", iter.Get())
	}

	if err := iter.Error(); err != nil {
		panic(err)
	}

}
Output:

Animal: <dog>
Animal: <cat>
Animal: <fox>
Animal: <pigeon>

func New

func New[T any](s []T) Iterator[T]

New returns an implementation of Iterator that traverses over the provided slice. The iterator returned supports the SizeHint itnerface.

func (*Iterator[T]) Error

func (r *Iterator[T]) Error() error

Error returns the context's error if the context is cancelled during a call to Next()

func (*Iterator[T]) Get

func (r *Iterator[T]) Get() T

Get returns element of the underlying slice that the iterator refers to

The context is not used in this iterator implementation.

func (*Iterator[T]) Next

func (r *Iterator[T]) Next(ctx context.Context) bool

Next advances the iterator to the next element of the underlying slice. It returns false when the end of the slice has been reached or the context is cancelled.

func (*Iterator[T]) Size

func (t *Iterator[T]) Size() uint

Size returns the length of the underlying slice, implementing the SizeHint interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL