itx

package
v2.0.0-beta.1 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2024 License: MIT Imports: 4 Imported by: 8

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Iterator

type Iterator[V any] iter.Seq[V]

Iterator is a wrapper around iter.Seq that allows for method chaining of most iterators found in the `it` package.

func Exhausted

func Exhausted[V any]() Iterator[V]

Exhausted is an iterator that yields no values.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/v2/it/itx"
)

func main() {
	fmt.Println(len(itx.Exhausted[int]().Collect()))
}
Output:

0

func From

func From[V any](iterator func(func(V) bool)) Iterator[V]

From converts an iterator in an Iterator to support method chaining.

func FromChannel

func FromChannel[V any](channel <-chan V) Iterator[V]

FromChannel yields values from a channel.

In order to avoid a deadlock, the channel must be closed before attempting to called `stop` on a pull-style iterator.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/v2/it/itx"
)

func main() {
	items := make(chan int)

	go func() {
		defer close(items)
		items <- 1
		items <- 2
	}()

	for number := range itx.FromChannel(items) {
		fmt.Println(number)
	}

}
Output:

1
2

func Integers

func Integers[V ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~int | ~int8 | ~int16 | ~int32 | ~int64](start, stop, step V) Iterator[V]

Integers yields all integers in the range [start, stop) with the given step.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/v2/it/itx"
)

func main() {
	fmt.Println(itx.Integers[uint](0, 3, 1).Collect())
}
Output:

[0 1 2]

func NaturalNumbers

func NaturalNumbers[V ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~int | ~int8 | ~int16 | ~int32 | ~int64]() Iterator[V]

NaturalNumbers yields all non-negative integers in ascending order.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/v2/it/itx"
)

func main() {
	fmt.Println(itx.NaturalNumbers[int]().Take(4).Collect())
}
Output:

[0 1 2 3]

func Once

func Once[V any](value V) Iterator[V]

Once yields the provided value once.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/v2/it/itx"
)

func main() {
	fmt.Println(itx.Once(42).Chain(itx.Once(43)).Collect())
}
Output:

[42 43]

func Repeat

func Repeat[V any](value V) Iterator[V]

Repeat yields the same value indefinitely.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/v2/it/itx"
)

func main() {
	fmt.Println(itx.Repeat(1).Take(3).Collect())
}
Output:

[1 1 1]

func (Iterator[V]) Chain

func (iterator Iterator[V]) Chain(iterators ...func(func(V) bool)) Iterator[V]

Chain is a convenience method for chaining it.Chain on [Iterator]s.

Example
package main

import (
	"fmt"
	"slices"

	"github.com/BooleanCat/go-functional/v2/it/itx"
)

func main() {
	numbers := itx.From(slices.Values([]int{1, 2})).Chain(slices.Values([]int{3, 4})).Collect()

	fmt.Println(numbers)
}
Output:

[1 2 3 4]

func (Iterator[V]) Collect

func (iterator Iterator[V]) Collect() []V

Collect is a convenience method for chaining slices.Collect on [Iterator]s.

Example
package main

import (
	"fmt"
	"slices"

	"github.com/BooleanCat/go-functional/v2/it/itx"
)

func main() {
	fmt.Println(itx.From(slices.Values([]int{1, 2, 3})).Collect())
}
Output:

[1 2 3]

func (Iterator[V]) Cycle

func (iterator Iterator[V]) Cycle() Iterator[V]

Cycle is a convenience method for chaining it.Cycle on [Iterator]s.

Example
package main

import (
	"fmt"
	"slices"

	"github.com/BooleanCat/go-functional/v2/it/itx"
)

func main() {
	numbers := itx.From(slices.Values([]int{1, 2})).Cycle().Take(5).Collect()

	fmt.Println(numbers)
}
Output:

[1 2 1 2 1]

func (Iterator[V]) Drop

func (iterator Iterator[V]) Drop(count uint) Iterator[V]

Drop is a convenience method for chaining it.Drop on [Iterator]s.

Example
package main

import (
	"fmt"
	"slices"

	"github.com/BooleanCat/go-functional/v2/it/itx"
)

func main() {
	for value := range itx.From(slices.Values([]int{1, 2, 3, 4, 5})).Drop(2) {
		fmt.Println(value)
	}

}
Output:

3
4
5

func (Iterator[V]) Enumerate

func (iterator Iterator[V]) Enumerate() Iterator2[int, V]

Enumerate is a convenience method for chaining it.Enumerate on [Iterator]s.

Example
package main

import (
	"fmt"
	"slices"

	"github.com/BooleanCat/go-functional/v2/it/itx"
)

func main() {
	for index, value := range itx.From(slices.Values([]int{1, 2, 3})).Enumerate() {
		fmt.Println(index, value)
	}

}
Output:

0 1
1 2
2 3

func (Iterator[V]) Exclude

func (iterator Iterator[V]) Exclude(predicate func(V) bool) Iterator[V]

Exclude is a convenience method for chaining it.Exclude on [Iterator]s.

Example
package main

import (
	"fmt"
	"slices"

	"github.com/BooleanCat/go-functional/v2/it/filter"
	"github.com/BooleanCat/go-functional/v2/it/itx"
)

func main() {
	for number := range itx.From(slices.Values([]int{1, 2, 3, 4, 5})).Exclude(filter.IsEven) {
		fmt.Println(number)
	}

}
Output:

1
3
5

func (Iterator[V]) Filter

func (iterator Iterator[V]) Filter(predicate func(V) bool) Iterator[V]

Filter is a convenience method for chaining it.Filter on [Iterator]s.

Example
package main

import (
	"fmt"
	"slices"

	"github.com/BooleanCat/go-functional/v2/it/filter"
	"github.com/BooleanCat/go-functional/v2/it/itx"
)

func main() {
	for number := range itx.From(slices.Values([]int{1, 2, 3, 4, 5})).Filter(filter.IsEven) {
		fmt.Println(number)
	}

}
Output:

2
4

func (Iterator[V]) Find

func (iterator Iterator[V]) Find(predicate func(V) bool) (V, bool)

Find is a convenience method for chaining it.Find on [Iterator]s.

Example
package main

import (
	"fmt"
	"slices"

	"github.com/BooleanCat/go-functional/v2/it/itx"
)

func main() {
	fmt.Println(itx.From(slices.Values([]int{1, 2, 3})).Find(func(number int) bool {
		return number == 2
	}))
}
Output:

2 true

func (Iterator[V]) ForEach

func (iterator Iterator[V]) ForEach(fn func(V))

ForEach is a convenience method for chaining it.ForEach on [Iterator]s.

Example
package main

import (
	"fmt"
	"slices"

	"github.com/BooleanCat/go-functional/v2/it/itx"
)

func main() {
	itx.From(slices.Values([]int{1, 2, 3})).ForEach(func(number int) {
		fmt.Println(number)
	})
}
Output:

1
2
3

func (Iterator[V]) Seq

func (iterator Iterator[V]) Seq() iter.Seq[V]

Seq converts an Iterator to an iter.Seq.

Example
package main

import (
	"fmt"
	"slices"

	"github.com/BooleanCat/go-functional/v2/it/itx"
)

func main() {
	fmt.Println(slices.Collect(itx.NaturalNumbers[int]().Take(3).Seq()))
}
Output:

[0 1 2]

func (Iterator[V]) Take

func (iterator Iterator[V]) Take(limit uint) Iterator[V]

Take is a convenience method for chaining it.Take on [Iterator]s.

Example
package main

import (
	"fmt"
	"slices"

	"github.com/BooleanCat/go-functional/v2/it/itx"
)

func main() {
	for number := range itx.From(slices.Values([]int{1, 2, 3, 4, 5})).Take(3) {
		fmt.Println(number)
	}

}
Output:

1
2
3

func (Iterator[V]) ToChannel

func (iterator Iterator[V]) ToChannel() <-chan V

ToChannel is a convenience method for chaining it.ToChannel on [Iterator]s.

Example
package main

import (
	"fmt"
	"slices"

	"github.com/BooleanCat/go-functional/v2/it/itx"
)

func main() {
	channel := itx.From(slices.Values([]int{1, 2, 3})).ToChannel()

	for number := range channel {
		fmt.Println(number)
	}

}
Output:

1
2
3

type Iterator2

type Iterator2[V, W any] iter.Seq2[V, W]

Iterator2 is a wrapper around iter.Seq2 that allows for method chaining of most iterators found in the `it` package.

func Exhausted2

func Exhausted2[V, W any]() Iterator2[V, W]

Exhausted2 is an iterator that yields no values.

Example
package main

import (
	"fmt"
	"maps"

	"github.com/BooleanCat/go-functional/v2/it/itx"
)

func main() {
	fmt.Println(len(maps.Collect(itx.Exhausted2[int, string]().Seq())))
}
Output:

0

func From2

func From2[V, W any](iterator func(func(V, W) bool)) Iterator2[V, W]

From2 converts an iterator in an Iterator2 to support method chaining.

func Lines

func Lines(r io.Reader) Iterator2[[]byte, error]

Lines yields lines from an io.Reader.

Note: lines longer than 65536 will cauese an error.

Example
package main

import (
	"fmt"
	"strings"

	"github.com/BooleanCat/go-functional/v2/it/itx"
)

func main() {
	for line := range itx.Lines(strings.NewReader("one\ntwo\nthree\n")) {
		fmt.Println(string(line))
	}
}
Output:

one
two
three

func LinesString

func LinesString(r io.Reader) Iterator2[string, error]

LinesString yields lines from an io.Reader as strings.

Note: lines longer than 65536 will cauese an error.

Example
package main

import (
	"fmt"
	"strings"

	"github.com/BooleanCat/go-functional/v2/it/filter"
	"github.com/BooleanCat/go-functional/v2/it/itx"
)

func main() {
	reader := strings.NewReader("one\ntwo\n\nthree\n")

	fmt.Println(itx.LinesString(reader).Left().Exclude(filter.IsZero[string]).Collect())
}
Output:

[one two three]

func Once2

func Once2[V, W any](v V, w W) Iterator2[V, W]

Once2 yields the provided value pair once.

Example
package main

import (
	"fmt"
	"maps"

	"github.com/BooleanCat/go-functional/v2/it/itx"
)

func main() {
	numbers := maps.Collect(itx.Once2(1, 42).Chain(itx.Once2(2, 43)).Seq())
	fmt.Println(numbers[1])
	fmt.Println(numbers[2])

}
Output:

42
43

func Repeat2

func Repeat2[V, W any](value1 V, value2 W) Iterator2[V, W]

Repeat2 yields the same two values indefinitely.

Example
package main

import (
	"fmt"
	"maps"

	"github.com/BooleanCat/go-functional/v2/it/itx"
)

func main() {
	fmt.Println(maps.Collect(itx.Repeat2(1, 2).Take(5).Seq()))
}
Output:

map[1:2]

func (Iterator2[V, W]) Chain

func (iterator Iterator2[V, W]) Chain(iterators ...func(func(V, W) bool)) Iterator2[V, W]

Chain is a convenience method for chaining it.Chain2 on [Iterator2]s.

Example
package main

import (
	"fmt"
	"iter"
	"maps"

	"github.com/BooleanCat/go-functional/v2/it/itx"
)

func main() {
	pairs := itx.From2(maps.All(map[string]int{"a": 1})).Chain(maps.All(map[string]int{"b": 2}))

	fmt.Println(len(maps.Collect(iter.Seq2[string, int](pairs))))
}
Output:

2

func (Iterator2[V, W]) Cycle

func (iterator Iterator2[V, W]) Cycle() Iterator2[V, W]

Cycle is a convenience method for chaining it.Cycle2 on [Iterator2]s.

Example
package main

import (
	"fmt"
	"iter"
	"maps"

	"github.com/BooleanCat/go-functional/v2/it/itx"
)

func main() {
	numbers := itx.Iterator2[int, string](maps.All(map[int]string{1: "one"})).Cycle().Take(5)

	fmt.Println(maps.Collect(iter.Seq2[int, string](numbers)))
}
Output:

map[1:one]

func (Iterator2[V, W]) Drop

func (iterator Iterator2[V, W]) Drop(count uint) Iterator2[V, W]

Drop is a convenience method for chaining it.Drop2 on [Iterator2]s.

Example
package main

import (
	"fmt"
	"maps"

	"github.com/BooleanCat/go-functional/v2/it/itx"
)

func main() {
	numbers := itx.From2(maps.All(map[int]string{1: "one", 2: "two", 3: "three"})).Drop(1)

	fmt.Println(len(maps.Collect(numbers.Seq())))
}
Output:

2

func (Iterator2[V, W]) Exclude

func (iterator Iterator2[V, W]) Exclude(predicate func(V, W) bool) Iterator2[V, W]

Exclude is a convenience method for chaining it.Exclude2 on [Iterator2]s.

Example
package main

import (
	"fmt"
	"maps"

	"github.com/BooleanCat/go-functional/v2/it/itx"
)

func main() {
	isOne := func(n int, _ string) bool { return n == 1 }
	numbers := map[int]string{1: "one", 3: "three"}

	for key, value := range itx.From2(maps.All(numbers)).Exclude(isOne) {
		fmt.Println(key, value)
	}

}
Output:

3 three

func (Iterator2[V, W]) Filter

func (iterator Iterator2[V, W]) Filter(predicate func(V, W) bool) Iterator2[V, W]

Filter is a convenience method for chaining it.Filter2 on [Iterator2]s.

Example
package main

import (
	"fmt"
	"maps"

	"github.com/BooleanCat/go-functional/v2/it/itx"
)

func main() {
	isOne := func(n int, _ string) bool { return n == 1 }
	numbers := map[int]string{1: "one", 2: "two", 3: "three"}

	for key, value := range itx.From2(maps.All(numbers)).Filter(isOne) {
		fmt.Println(key, value)
	}

}
Output:

1 one

func (Iterator2[V, W]) Find

func (iterator Iterator2[V, W]) Find(predicate func(V, W) bool) (V, W, bool)

Find is a convenience method for chaining it.Find2 on [Iterator2]s.

Example
package main

import (
	"fmt"
	"slices"

	"github.com/BooleanCat/go-functional/v2/it/itx"
)

func main() {
	fmt.Println(itx.From(slices.Values([]int{1, 2, 3})).Enumerate().Find(func(index int, number int) bool {
		return index == 1
	}))
}
Output:

1 2 true

func (Iterator2[V, W]) ForEach

func (iterator Iterator2[V, W]) ForEach(fn func(V, W))

ForEach is a convenience method for chaining it.ForEach2 on [Iterator2]s.

Example
package main

import (
	"fmt"
	"slices"

	"github.com/BooleanCat/go-functional/v2/it/itx"
)

func main() {
	itx.From(slices.Values([]int{1, 2, 3})).Enumerate().ForEach(func(index int, number int) {
		fmt.Println(index, number)
	})
}
Output:

0 1
1 2
2 3

func (Iterator2[V, W]) Left

func (iterator Iterator2[V, W]) Left() Iterator[V]

Left is a convenience method that unzips an Iterator2 and returns the left iterator, closing the right iterator.

Example
package main

import (
	"fmt"
	"strings"

	"github.com/BooleanCat/go-functional/v2/it/itx"
)

func main() {
	text := strings.NewReader("one\ntwo\nthree\n")

	fmt.Println(itx.LinesString(text).Left().Collect())
}
Output:

[one two three]

func (Iterator2[V, W]) Right

func (iterator Iterator2[V, W]) Right() Iterator[W]

Right is a convenience method that unzips an Iterator2 and returns the right iterator, closing the left iterator.

Example
package main

import (
	"fmt"
	"maps"

	"github.com/BooleanCat/go-functional/v2/it/itx"
)

func main() {
	for value := range itx.From2(maps.All(map[int]string{1: "one"})).Right() {
		fmt.Println(value)
	}
}
Output:

one

func (Iterator2[V, W]) Seq

func (iterator Iterator2[V, W]) Seq() iter.Seq2[V, W]

Seq converts an Iterator2 to an iter.Seq2.

Example
package main

import (
	"fmt"
	"maps"

	"github.com/BooleanCat/go-functional/v2/it/itx"
)

func main() {
	fmt.Println(maps.Collect(itx.From2(maps.All(map[int]int{1: 2})).Seq()))
}
Output:

map[1:2]

func (Iterator2[V, W]) Take

func (iterator Iterator2[V, W]) Take(limit uint) Iterator2[V, W]

Take is a convenience method for chaining it.Take2 on [Iterator2]s.

Example
package main

import (
	"fmt"
	"maps"

	"github.com/BooleanCat/go-functional/v2/it/itx"
)

func main() {
	numbers := maps.Collect(itx.From2(maps.All(map[int]string{1: "one", 2: "two", 3: "three"})).Take(2).Seq())

	fmt.Println(len(numbers))
}
Output:

2

func (Iterator2[V, W]) Unzip

func (iterator Iterator2[V, W]) Unzip() (Iterator[V], Iterator[W])

Unzip is a convenience method for chaining it.Unzip on [Iterator2]s.

Example
package main

import (
	"fmt"
	"maps"

	"github.com/BooleanCat/go-functional/v2/it/itx"
)

func main() {
	keys, values := itx.From2(maps.All(map[int]string{1: "one", 2: "two"})).Unzip()

	for key := range keys {
		fmt.Println(key)
	}

	for value := range values {
		fmt.Println(value)
	}
}

Jump to

Keyboard shortcuts

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