itx

package
v2.4.0 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2024 License: MIT Imports: 5 Imported by: 7

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] func(func(V) bool)

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.

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 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/filter"
	"github.com/BooleanCat/go-functional/v2/it/itx"
)

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

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

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

}
Output:

1
2

func FromSlice

func FromSlice[V any](slice []V) Iterator[V]

FromSlice converts a slice to an Iterator.

Example
package main

import (
	"fmt"

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

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

[1 2 3]

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"

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

func main() {
	numbers := itx.FromSlice([]int{1, 2}).Chain(itx.FromSlice([]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"

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

func main() {
	fmt.Println(itx.FromSlice([]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"

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

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

	fmt.Println(numbers)
}
Output:

[1 2 1 2 1]

func (Iterator[V]) Drain added in v2.1.0

func (iterator Iterator[V]) Drain()

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

Example
package main

import (
	"fmt"
	"slices"

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

func main() {
	itx.From(it.Map(slices.Values([]int{1, 2, 3}), func(n int) int {
		fmt.Println(n)
		return n
	})).Drain()

}
Output:

1
2
3

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"

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

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

}
Output:

3
4
5

func (Iterator[V]) DropWhile

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

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

Example
package main

import (
	"fmt"

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

func main() {
	for value := range itx.FromSlice([]int{1, 2, 3, 4, 5}).DropWhile(filter.LessThan(3)) {
		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"

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

func main() {
	for index, value := range itx.FromSlice([]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"

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

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

}
Output:

1
3
5

func (Iterator[V]) ExcludeError

func (iterator Iterator[V]) ExcludeError(predicate func(V) (bool, error)) Iterator2[V, error]

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

Example
package main

import (
	"fmt"

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

func main() {
	isEven := func(n int) (bool, error) { return n%2 == 0, nil }

	odds, err := it.TryCollect(itx.FromSlice([]int{1, 2, 3, 4, 5}).ExcludeError(isEven))
	if err == nil {
		fmt.Println(odds)
	}

}
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"

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

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

}
Output:

2
4

func (Iterator[V]) FilterError

func (iterator Iterator[V]) FilterError(predicate func(V) (bool, error)) Iterator2[V, error]

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

Example
package main

import (
	"fmt"

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

func main() {
	isEven := func(n int) (bool, error) { return n%2 == 0, nil }

	evens, err := it.TryCollect(itx.FromSlice([]int{1, 2, 3, 4, 5}).FilterError(isEven))
	if err == nil {
		fmt.Println(evens)
	}

}
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"

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

func main() {
	fmt.Println(itx.FromSlice([]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"

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

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

1
2
3

func (Iterator[V]) Len

func (iterator Iterator[V]) Len() int

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

Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(itx.FromSlice([]int{1, 2, 3}).Len())
}
Output:

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"

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

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

}
Output:

1
2
3

func (Iterator[V]) TakeWhile

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

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

Example
package main

import (
	"fmt"

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

func main() {
	for number := range itx.FromSlice([]int{1, 2, 3, 4, 5}).TakeWhile(filter.LessThan(4)) {
		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"

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

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

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

}
Output:

1
2
3

func (Iterator[V]) Transform

func (iterator Iterator[V]) Transform(f func(V) V) Iterator[V]

Transform is a convenience method for chaining it.Map on [Iterator]s where the provided functions argument type is the same as its return type.

This is a limited version of it.Map due to a limitation on Go's type system whereby new generic type parameters cannot be defined on methods.

Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(itx.FromSlice([]int{0, 1, 2}).Transform(func(v int) int {
		return v + 1
	}).Collect())
}
Output:

[1 2 3]

func (Iterator[V]) TransformError

func (iterator Iterator[V]) TransformError(f func(V) (V, error)) Iterator2[V, error]

TransformError is a convenience method for chaining it.MapError on [Iterator]s where the provided functions argument type is the same as its return type.

This is a limited version of it.MapError due to a limitation on Go's type system whereby new generic type parameters cannot be defined on methods.

Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(it.TryCollect(itx.FromSlice([]int{0, 1, 2}).TransformError(func(v int) (int, error) {
		return v + 1, nil
	})))
}
Output:

[1 2 3] <nil>

type Iterator2

type Iterator2[V, W any] func(func(V, W) bool)

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"

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

func main() {
	fmt.Println(len(itx.Exhausted2[int, string]().Left().Collect()))
}
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.

Example
package main

import (
	"fmt"
	"maps"

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

func main() {
	numbers := maps.All(map[int]string{1: "one"})
	fmt.Println(maps.Collect(itx.From2(numbers).Seq()))
}
Output:

map[1:one]

func FromMap

func FromMap[V comparable, W any](m map[V]W) Iterator2[V, W]

FromMap converts a map to an Iterator2.

Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(itx.FromMap(map[int]int{1: 2}).Right().Collect())
}
Output:

[2]

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"
	"maps"

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

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

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

2

func (Iterator2[V, W]) Collect

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

Collect2 consumes an iter.Seq2 and returns two slices of values.

Example
package main

import (
	"fmt"

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

func main() {
	indicies, values := itx.FromSlice([]int{1, 2, 3}).Enumerate().Collect()
	fmt.Println(values)
	fmt.Println(indicies)

}
Output:

[1 2 3]
[0 1 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"
	"maps"

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

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

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

map[1:one]

func (Iterator2[V, W]) Drain added in v2.1.0

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

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

Example
package main

import (
	"fmt"
	"slices"

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

func main() {
	itx.From2(it.Map2(slices.All([]int{1, 2, 3}), func(i, n int) (int, int) {
		fmt.Println(n)
		return i, n
	})).Drain()

}
Output:

1
2
3

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.FromMap(map[int]string{1: "one", 2: "two", 3: "three"}).Drop(1)

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

2

func (Iterator2[V, W]) DropWhile

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

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

Example
package main

import (
	"fmt"

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

func main() {
	lessThanThree := func(int, v int) bool { return v < 3 }

	_, numbers := itx.FromSlice([]int{1, 2, 3, 4, 5}).Enumerate().DropWhile(lessThanThree).Collect()
	fmt.Println(numbers)
}
Output:

[3 4 5]

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"

	"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.FromMap(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"

	"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.FromMap(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"

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

func main() {
	fmt.Println(itx.FromSlice([]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"

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

func main() {
	itx.FromSlice([]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]) Len

func (iterator Iterator2[V, W]) Len() int

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

Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(itx.FromSlice([]int{1, 2, 3}).Enumerate().Len())
}
Output:

3

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"

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

func main() {
	for value := range itx.FromMap(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.FromMap(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.FromMap(map[int]string{1: "one", 2: "two", 3: "three"}).Take(2).Seq())

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

2

func (Iterator2[V, W]) TakeWhile

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

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

Example
package main

import (
	"fmt"

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

func main() {
	lessThanFour := func(int, v int) bool { return v < 4 }

	_, numbers := itx.FromSlice([]int{1, 2, 3, 4, 5}).Enumerate().TakeWhile(lessThanFour).Collect()
	fmt.Println(numbers)
}
Output:

[1 2 3]

func (Iterator2[V, W]) Transform

func (iterator Iterator2[V, W]) Transform(f func(V, W) (V, W)) Iterator2[V, W]

Transform is a convenience method for chaining it.Map2 on [Iterator2]s where the provided functions argument type is the same as its return type.

This is a limited version of it.Map2 due to a limitation on Go's type system whereby new generic type parameters cannot be defined on methods.

Example
package main

import (
	"fmt"
	"maps"

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

func main() {
	addOne := func(a, b int) (int, int) {
		return a + 1, b + 1
	}

	fmt.Println(maps.Collect(itx.FromMap(map[int]int{1: 2}).Transform(addOne).Seq()))
}
Output:

map[2:3]

Jump to

Keyboard shortcuts

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