transform2

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2024 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Overview

Package transform2 provides functions that transform seq2[K, V] iterators in various ways.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Backwards

func Backwards[K, V any](seq iter.Seq2[K, V]) iter.Seq2[K, V]

Backwards returns a Seq2[K, V] with the values of the input iterator reversed. It takes O(n) time and space to collect all the values.

Example
package main

import (
	"fmt"

	"github.com/elordeiro/goext/seq2s/transform2"
	"github.com/elordeiro/goext/seqs"
)

func main() {
	seq2 := seqs.Enumerate(0, seqs.Range(5))
	for k, v := range transform2.Backwards(seq2) {
		fmt.Printf("%d:%d ", k, v)
	}
	fmt.Println()
}
Output:

4:4 3:3 2:2 1:1 0:0

func DropWhile

func DropWhile[K, V any](seq iter.Seq2[K, V], predicate func(K, V) bool) iter.Seq2[K, V]

DropWhile returns a Seq2[K, V] that drops values from the input iterator until the predicate is false.

Example
package main

import (
	"fmt"

	"github.com/elordeiro/goext/seq2s/transform2"
	"github.com/elordeiro/goext/seqs"
)

func main() {
	isLessThan5 := func(k, v int) bool { return v < 5 }
	seq2 := seqs.Enumerate(0, seqs.Range(10))
	for k, v := range transform2.DropWhile(seq2, isLessThan5) {
		fmt.Printf("%d:%d ", k, v)
	}
	fmt.Println()
}
Output:

5:5 6:6 7:7 8:8 9:9

func Filter

func Filter[K, V any](seq iter.Seq2[K, V], filter func(K, V) bool) iter.Seq2[K, V]

Filter returns a Seq2[K, V] over key-value pairs that satisfy the filter function.

Example
package main

import (
	"fmt"

	"github.com/elordeiro/goext/seq2s/transform2"
	"github.com/elordeiro/goext/seqs"
)

func main() {
	isEven := func(k, v int) bool { return v%2 == 0 }
	seq2 := seqs.Enumerate(0, seqs.Range(5))
	for k, v := range transform2.Filter(seq2, isEven) {
		fmt.Printf("%d:%d ", k, v)
	}
	fmt.Println()
}
Output:

0:0 2:2 4:4
Example (Inline)
package main

import (
	"fmt"

	"github.com/elordeiro/goext/seq2s/transform2"
	"github.com/elordeiro/goext/seqs"
)

func main() {
	seq2 := seqs.Enumerate(0, seqs.Range(5))
	for k, v := range transform2.Filter(seq2, func(k, v int) bool { return v%2 == 0 }) {
		fmt.Printf("%d:%d ", k, v)
	}
	fmt.Println()
}
Output:

0:0 2:2 4:4

func ForEach

func ForEach[K, V any](seq iter.Seq2[K, V], do func(K, V))

ForEach applies a function to each key-value pair in the iterator. This function is not lazy and will consume the entire iterator.

Example
package main

import (
	"fmt"
	"slices"

	"github.com/elordeiro/goext/containers/tuples"
	"github.com/elordeiro/goext/seq2s/transform2"
	"github.com/elordeiro/goext/seqs/transform"
)

func main() {
	seq2 := transform.Unpair(
		slices.Values(
			tuples.Pairs(
				tuples.NewPair(1, 2),
				tuples.NewPair(3, 4),
				tuples.NewPair(5, 6),
			),
		),
	)
	transform2.ForEach(seq2, func(k, v int) {
		fmt.Printf("%d:%d ", k, v*2)
	})
	fmt.Println()
}
Output:

1:4 3:8 5:12

func Map

func Map[K, V any](seq iter.Seq2[K, V], transform func(K, V) (K, V)) iter.Seq2[K, V]

Map returns a Seq2[K, V] over key-value pairs that are transformed by the map function.

Example
package main

import (
	"fmt"

	"github.com/elordeiro/goext/seq2s/transform2"
	"github.com/elordeiro/goext/seqs"
)

func main() {
	double := func(k, v int) (int, int) { return k, v * 2 }
	seq2 := seqs.Enumerate(0, seqs.Range(5))
	for k, v := range transform2.Map(seq2, double) {
		fmt.Printf("%d:%d ", k, v)
	}
	fmt.Println()
}
Output:

0:0 1:2 2:4 3:6 4:8
Example (Inline)
package main

import (
	"fmt"

	"github.com/elordeiro/goext/seq2s/transform2"
	"github.com/elordeiro/goext/seqs"
)

func main() {
	seq2 := seqs.Enumerate(0, seqs.Range(5))
	for k, v := range transform2.Map(seq2, func(k, v int) (int, int) { return k, v * v }) {
		fmt.Printf("%d:%d ", k, v)
	}
	fmt.Println()
}
Output:

0:0 1:1 2:4 3:9 4:16

func OnEmpty

func OnEmpty[K, V any](seq iter.Seq2[K, V], callback func()) iter.Seq2[K, V]

OnEmpty returns a Seq2[K, V] that calls an else function only if the iterator is exhausted.

Example
package main

import (
	"fmt"
	"slices"

	"github.com/elordeiro/goext/containers/tuples"
	"github.com/elordeiro/goext/seq2s"
	"github.com/elordeiro/goext/seq2s/transform2"
	"github.com/elordeiro/goext/seqs/transform"
)

func main() {
	slice := tuples.Pairs(
		tuples.NewPair(1, 2),
		tuples.NewPair(3, 4),
		tuples.NewPair(5, 6),
	)
	seq := transform.Unpair(slices.Values(slice))
	Else := func() { slice = tuples.Pairs(tuples.NewPair(100, 200)) }
	for i, p := range seq2s.Enumerate(0, transform2.OnEmpty(seq, Else)) {
		if p.Left() == 5 {
			break
		}
		slice[i] = tuples.NewPair(p.Left(), p.Right()*i)
	}
	fmt.Println(slice)
}
Output:

[(1 0) (3 4) (5 6)]
Example (True)
package main

import (
	"fmt"
	"slices"

	"github.com/elordeiro/goext/containers/tuples"
	"github.com/elordeiro/goext/seq2s"
	"github.com/elordeiro/goext/seq2s/transform2"
	"github.com/elordeiro/goext/seqs/transform"
)

func main() {
	slice := tuples.Pairs(
		tuples.NewPair(1, 2),
		tuples.NewPair(3, 4),
		tuples.NewPair(5, 6),
	)

	seq := transform.Unpair(slices.Values(slice))
	Else := func() { slice = tuples.Pairs(tuples.NewPair(100, 200)) }
	for i, p := range seq2s.Enumerate(0, transform2.OnEmpty(seq, Else)) {
		if p.Right() == 10 {
			break
		}
		slice[i] = tuples.NewPair(p.Left(), p.Right()*i)
	}
	fmt.Println(slice)
}
Output:

[(100 200)]

func Reduce

func Reduce[K, V, A any](seq iter.Seq2[K, V], reduce func(A, V) A, start ...A) A

Reduce returns a single value that is the result of applying the reduce function to all values in the iterator. The function also accepts an optional starting value for the accumulator.

Example
package main

import (
	"fmt"

	"github.com/elordeiro/goext/seq2s/transform2"
	"github.com/elordeiro/goext/seqs"
)

func main() {
	sum := func(acc, v int) int { return acc + v }
	seq2 := seqs.Enumerate(0, seqs.Range(5))
	result := transform2.Reduce(seq2, sum)
	fmt.Println(result)
}
Output:

10
Example (Start)
package main

import (
	"fmt"

	"github.com/elordeiro/goext/seq2s/transform2"
	"github.com/elordeiro/goext/seqs"
)

func main() {
	sum := func(acc, v int) int { return acc + v }
	seq2 := seqs.Enumerate(0, seqs.Range(5))
	result := transform2.Reduce(seq2, sum, 10)
	fmt.Println(result)
}
Output:

20

func Rotate

func Rotate[I constraints.Integer, K, V any](n I, seq iter.Seq2[K, V]) iter.Seq2[K, V]

Rotate returns a Seq[V] with the values of the input rotated left by n steps. If n < 1 the function returns the same sequence. If n > Len(seq), the resulting sequence will be the same as the input sequence.

Example
package main

import (
	"fmt"

	"github.com/elordeiro/goext/seq2s/transform2"
	"github.com/elordeiro/goext/seqs"
)

func main() {
	for k, v := range transform2.Rotate(2, seqs.Enumerate(0, seqs.Range(5))) {
		fmt.Printf("%d:%d ", k, v)
	}
	fmt.Println()
}
Output:

2:2 3:3 4:4 0:0 1:1

func SwapKV

func SwapKV[K, V any](seq iter.Seq2[K, V]) iter.Seq2[V, K]

SwapKV returns a Seq2[V, K] that swaps the keys and values of the input iterator.

Example
package main

import (
	"fmt"
	"slices"

	"github.com/elordeiro/goext/seq2s/transform2"
	"github.com/elordeiro/goext/seqs"
)

func main() {
	seq1 := slices.Values([]int{1, 2, 3, 4, 5})
	seq2 := slices.Values([]string{"a", "b", "c", "d", "e"})
	seq := seqs.Zip(seq1, seq2)
	for k, v := range transform2.SwapKV(seq) {
		fmt.Print(k, ":", v, " ")
	}
	fmt.Println()
}
Output:

a:1 b:2 c:3 d:4 e:5

func TakeWhile

func TakeWhile[K, V any](seq iter.Seq2[K, V], predicate func(K, V) bool) iter.Seq2[K, V]

TakeWhile returns a Seq2[K, V] that yields values from the input iterator until the predicate is false.

Example
package main

import (
	"fmt"

	"github.com/elordeiro/goext/seq2s/transform2"
	"github.com/elordeiro/goext/seqs"
)

func main() {
	isLessThan5 := func(k, v int) bool { return v < 5 }
	seq2 := seqs.Enumerate(0, seqs.Range(10))
	for k, v := range transform2.TakeWhile(seq2, isLessThan5) {
		fmt.Printf("%d:%d ", k, v)
	}
	fmt.Println()
}
Output:

0:0 1:1 2:2 3:3 4:4

func With

func With[K, V any](seq iter.Seq2[K, V], process func(K, V)) iter.Seq2[K, V]

With returns a Seq2[K, V] that calls a function on each iteration before yielding it.

Example
package main

import (
	"fmt"
	"slices"

	"github.com/elordeiro/goext/containers/tuples"
	"github.com/elordeiro/goext/seq2s/transform2"
	"github.com/elordeiro/goext/seqs/transform"
)

func main() {
	seq := slices.Values(tuples.Pairs(
		tuples.NewPair("alice", 1),
		tuples.NewPair("bob", 2),
		tuples.NewPair("charlie", 3),
		tuples.NewPair("david", 4),
		tuples.NewPair("eve", 5)))
	seq2 := transform.Unpair(seq)
	result := ""
	process := func(k string, v int) {
		if v > 3 {
			result += k + " "
		}
	}
	for range transform2.With(seq2, process) {
	}
	fmt.Println(result)
}
Output:

david eve

Types

This section is empty.

Jump to

Keyboard shortcuts

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