lazy

package module
v0.0.0-...-c13af6c Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2023 License: MIT Imports: 1 Imported by: 0

README

lazy

A lazy iterator for lazy programmers with emphasis on clarity, and decent performance.

Pick Your Source
// {1}
lazy.FromFunc(func() (int, bool) {
    return 1, false
})

// 1, 1, ...
lazy.Generate(func() int {
    return 1
})

lazy.FromValues("a", "b") // {"a", "b"}
lazy.FromSlice([]string{"a", "b", "c"}) // {"a", "b", "c"}

lazy.To(10) // [0, 10)
lazy.From(5) // [5, inf)
lazy.From(1).To(3) // {1, 2}
lazy.From(0).To(10).By(50) // {0, 50}
Chain Operations

Chain operations pointer free, interface free and gluten free:

import "github.com/gavraz/lazy"

it := lazy.Generate(func() int {
    return rand.Int() % 100
}).Filter(func(x int) bool {
    return x%2 == 1
}).Map(func(v int) int {
    return v * v
}).Limit(10)

for v, ok := it.Next(); ok; v, ok = it.Next() {
	fmt.Println(v)
}
// Output: 49 25 1 1 25 1 81 9 25 49

Current Operations:

  • Filter
  • Map
  • Paginate
  • Limit
  • Discard
Iterate Easily
it = lazy.(...).Easy() 
for it.Next() {
    // it.Value()
}

Or, turn into a slice:

s := it.Limit(10).Slice()
fmt.Println(cap(s)) // A best effort is made to allocate the exact size: cap(s) = 10

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Easy

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

func (*Easy[T]) Next

func (ez *Easy[T]) Next() bool

func (*Easy[T]) Value

func (ez *Easy[T]) Value() T

type Iterator

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

func FromFunc

func FromFunc[T any](next func() (T, bool)) Iterator[T]

func FromSlice

func FromSlice[T any](data []T) Iterator[T]

func FromValues

func FromValues[T any](v ...T) Iterator[T]

func Generate

func Generate[T any](rnd func() T) Iterator[T]

func Map

func Map[T any, S any](it Iterator[T], m func(T) S) Iterator[S]

func Range

func Range[T Numeric](begin, end, delta T) Iterator[T]

Range returns an iterator for a range [begin, begin+delta, ..., end). Values are assumed to be non-negative. Note: end=0 represents infinity.

func To

func To(end int) Iterator[int]

To returns an iterator for the range: [0, end)

func (Iterator[T]) Discard

func (it Iterator[T]) Discard(count int) Iterator[T]

func (Iterator[T]) Easy

func (it Iterator[T]) Easy() *Easy[T]

func (Iterator[T]) Filter

func (it Iterator[T]) Filter(filter func(T) bool) Iterator[T]

func (Iterator[T]) Limit

func (it Iterator[T]) Limit(limit int) Iterator[T]

func (Iterator[T]) Map

func (it Iterator[T]) Map(m func(T) T) Iterator[T]

func (Iterator[T]) Next

func (it Iterator[T]) Next() (T, bool)

func (Iterator[T]) Paginate

func (it Iterator[T]) Paginate(page, count int) Iterator[T]

func (Iterator[T]) Slice

func (it Iterator[T]) Slice() []T

type Numeric

type Numeric interface {
	constraints.Integer | constraints.Float
}

type Ranger

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

func From

func From[T Numeric](begin T) Ranger[T]

func (Ranger[T]) By

func (r Ranger[T]) By(delta T) Iterator[T]

func (Ranger[T]) To

func (r Ranger[T]) To(end T) Ranger[T]

Jump to

Keyboard shortcuts

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