itx

package
v2.0.0-alpha.5 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2024 License: MIT Imports: 4 Imported by: 8

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func MapsCollect

func MapsCollect[K comparable, V any](iterator Iterator2[K, V]) map[K]V

MapsCollect is a wrapper around maps.Collect accepting an Iterator2 rather than an iter.Seq2.

Note: there is no convenience method for chaining Collect on an it.Iterator2 because Go's type system will not allow for specifying a comparable constraint on a type parameter in the method definition.

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 Count

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

Count 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.Count[int]().Take(4).Collect())
}
Output:

[0 1 2 3]

func SlicesValues

func SlicesValues[Slice ~[]E, E any](s Slice) Iterator[E]

SlicesValues is a wrapper around slices.Values returning an Iterator rather than an iter.Seq.

func (Iterator[V]) Chain

func (iterator Iterator[V]) Chain(iterators ...Iterator[V]) 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.SlicesValues([]int{1, 2}).Chain(itx.SlicesValues([]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.SlicesValues([]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.SlicesValues([]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 int) 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.SlicesValues([]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"

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

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

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

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

}
Output:

2
4

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.SlicesValues([]int{1, 2, 3}).ForEach(func(number int) {
		fmt.Println(number)
	})
}
Output:

1
2
3

func (Iterator[V]) Take

func (iterator Iterator[V]) Take(limit int) 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.SlicesValues([]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"

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

func main() {
	channel := itx.SlicesValues([]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 MapsAll

func MapsAll[Map ~map[K]V, K comparable, V any](m Map) Iterator2[K, V]

MapsAll is a wrapper around maps.All returning an Iterator2 rather than an iter.Seq2.

func (Iterator2[V, W]) Chain

func (iterator Iterator2[V, W]) Chain(iterators ...Iterator2[V, W]) 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.MapsAll(map[string]int{"a": 1}).Chain(itx.MapsAll(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 int) Iterator2[V, W]

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

Example
package main

import (
	"fmt"

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

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

	fmt.Println(len(itx.MapsCollect(numbers)))
}
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"

	"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.MapsAll(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.MapsAll(numbers).Filter(isOne) {
		fmt.Println(key, value)
	}

}
Output:

1 one

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.SlicesValues([]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]) Take

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

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

Example
package main

import (
	"fmt"

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

func main() {
	numbers := itx.MapsCollect(itx.MapsAll(map[int]string{1: "one", 2: "two", 3: "three"}).Take(2))

	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"

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

func main() {
	keys, values := itx.MapsAll(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