Documentation
¶
Index ¶
- func AppendFunc[SliceE ~[]E, SliceV ~[]V, E, V any](se SliceE, sv SliceV, fn func(V) E) SliceE
- func AppendIf[Slice ~[]E, E any](s1, s2 Slice, predicate func(E) bool) Slice
- func AppendSeqFunc[Slice ~[]E, E, V any](s Slice, seq iter.Seq[V], fn func(V) E) Slice
- func AppendSeqIf[Slice ~[]E, E any](s Slice, seq iter.Seq[E], predicate func(E) bool) Slice
- func Filter[Slice ~[]E, E any](s Slice, predicate func(E) bool) Slice
- func FilterSeq[E any](seq iter.Seq[E], predicate func(E) bool) iter.Seq[E]
- func FoldLeft[Slice ~[]E, E, V any](s Slice, initial V, fn func(V, E) V) V
- func FoldRight[Slice ~[]E, E, V any](s Slice, initial V, fn func(V, E) V) V
- func IterFunc[E, V any](seq iter.Seq[E], fn func(E) V) iter.Seq[V]
- func Map[E, V any](s []E, fn func(E) V) []V
- func MapIf[E, V any](s []E, fn func(E) (V, bool)) []V
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AppendFunc ¶
func AppendFunc[SliceE ~[]E, SliceV ~[]V, E, V any](se SliceE, sv SliceV, fn func(V) E) SliceE
Append appends the results of applying fn to each element of s2 to s1 and returns the result.
Example ¶
package main
import (
"fmt"
"github.com/moeryomenko/xiter"
)
func main() {
// Create a sequence from a slice of integers
s1 := []int{1, 2, 3}
s2 := []int{4, 5, 6}
// Append the elements of s2 to s1 after multiplying by 2
appended := xiter.AppendFunc(s1, s2, func(v int) int { return v * 2 })
fmt.Println(appended)
}
Output: [1 2 3 8 10 12]
func AppendIf ¶
AppendIf appends the elements of s2 that satisfy the given predicate to the slice s1.
Example ¶
package main
import (
"fmt"
"github.com/moeryomenko/xiter"
)
func main() {
// Create a sequence from a slice of integers
s1 := []int{1, 2, 3}
s2 := []int{4, 5, 6}
// append only even numbers from s2 to s1
appended := xiter.AppendIf(s1, s2, func(v int) bool { return v%2 == 0 })
fmt.Println(appended)
}
Output: [1 2 3 4 6]
func AppendSeqFunc ¶
AppendSeqFunc appends the results of applying fn to each element in seq to the slice s.
func AppendSeqIf ¶
AppendSeqIf appends the elements of seq that satisfy the given predicate to the slice s.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/moeryomenko/xiter"
)
func main() {
// Create a sequence from a slice of integers
s1 := []int{1, 2, 3}
s2 := []int{4, 5, 6}
// Append elements from s2 to s1 that are greater than 4
appended := xiter.AppendSeqIf(s1, slices.Values(s2), func(v int) bool { return v > 4 })
fmt.Println(appended)
}
Output: [1 2 3 5 6]
func Filter ¶
Filter returns a new slice containing the elements of s that satisfy the given predicate.
Example ¶
package main
import (
"fmt"
"github.com/moeryomenko/xiter"
)
func main() {
// Create a sequence from a slice of integers
s := []int{1, 2, 3, 4, 5, 6}
// Filter even numbers from a slice
filtered := xiter.Filter(s, func(n int) bool { return n%2 == 0 })
fmt.Println(filtered)
}
Output: [2 4 6]
func FilterSeq ¶
FilterSeq returns a sequence that yields the elements of seq that satisfy the given predicate.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/moeryomenko/xiter"
)
func main() {
// Create a sequence from a slice of integers
words := slices.Values([]string{"apple", "banana", "cherry", "date"})
// Filter strings longer than 5 characters
filteredWords := xiter.FilterSeq(words, func(s string) bool { return len(s) > 5 })
fmt.Println(slices.Collect(filteredWords))
}
Output: [banana cherry]
func FoldLeft ¶ added in v0.0.3
func FoldLeft[Slice ~[]E, E, V any](s Slice, initial V, fn func(V, E) V) V
FoldLeft returns the result of repeatedly applying fn to the elements of s, from left to right.
Example ¶
package main
import (
"fmt"
"github.com/moeryomenko/xiter"
)
func main() {
seq := []int{1, 2, 3, 4}
// Fold the sequence to calculate the sum
sum := xiter.FoldLeft(seq, 0, func(acc, v int) int { return acc + v })
fmt.Println(sum)
}
Output: 10
func FoldRight ¶ added in v0.0.3
func FoldRight[Slice ~[]E, E, V any](s Slice, initial V, fn func(V, E) V) V
FoldRight returns the result of repeatedly applying fn to the elements of s, from right to left.
Example ¶
package main
import (
"fmt"
"github.com/moeryomenko/xiter"
)
func main() {
seq := []int{1, 2, 3, 4}
// Fold the sequence to calculate the sum
sum := xiter.FoldLeft(seq, 12, func(acc, v int) int { return acc - v })
fmt.Println(sum)
}
Output: 2
func IterFunc ¶
IterFunc returns a sequence that yields the results of applying fn to each element of seq.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/moeryomenko/xiter"
)
func main() {
// Create a sequence from a slice of integers
seq := slices.Values([]int{1, 2, 3})
// Apply a function that squares each element
squared := xiter.IterFunc(seq, func(e int) int { return e * e })
fmt.Println(slices.Collect(squared))
}
Output: [1 4 9]
func Map ¶ added in v0.0.2
func Map[E, V any](s []E, fn func(E) V) []V
Map returns a new slice containing the results of applying fn to each element of s.
Example ¶
package main
import (
"fmt"
"strconv"
"github.com/moeryomenko/xiter"
)
func main() {
intSlice := []int{1, 2, 3}
// Apply a function that convert to string each element
stringSlice := xiter.Map(intSlice, func(e int) string { return strconv.Itoa(e) })
fmt.Println(stringSlice)
}
Output: [1 2 3]
func MapIf ¶ added in v0.1.1
MapIf returns a new slice containing the results of applying fn to each element of s, but only if fn returns true.
Example ¶
package main
import (
"fmt"
"strconv"
"github.com/moeryomenko/xiter"
)
func main() {
intSlice := []int{1, 2, 3, 4, 5}
// Apply a function that convert to string each element if it is even
stringSlice := xiter.MapIf(intSlice, func(e int) (string, bool) { return strconv.Itoa(e), e%2 == 0 })
fmt.Println(stringSlice)
}
Output: [2 4]
Types ¶
This section is empty.