islices

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2024 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Filter

func Filter[T any](s iter.Seq[T], filterFunc FilterFunc[T]) iter.Seq[T]

Filter yields only values for which filterFunc returns true

Example
package main

import (
	"fmt"
	"slices"

	islices "github.com/gomoni/it/islices"
)

func main() {
	n := []string{"aa", "aaa", "aaaaaaa", "a"}
	s0 := slices.Values(n)
	s1 := islices.Filter(s0, func(s string) bool { return len(s) >= 2 })
	slice := slices.Collect(s1)
	fmt.Println(slice)
}
Output:

[aa aaa aaaaaaa]

func Map

func Map[T, V any](s iter.Seq[T], mapFunc MapFunc[T, V]) iter.Seq[V]

Map calls a mapping function on each member of the sequence

Example
package main

import (
	"fmt"
	"slices"
	"strconv"

	islices "github.com/gomoni/it/islices"
)

func main() {
	n := []string{"aa", "aaa", "aaaaaaa", "a"}
	// maps string->int->float32
	s0 := slices.Values(n)
	s1 := islices.Map(s0, func(s string) int { return len(s) })
	s2 := islices.Map(s1, func(i int) float32 { return float32(i) })
	s3 := islices.Map(s2, func(f float32) string { return strconv.FormatFloat(float64(f), 'E', 4, 32) })
	slice := slices.Collect(s3)
	fmt.Println(slice)
}
Output:

[2.0000E+00 3.0000E+00 7.0000E+00 1.0000E+00]

Types

type FilterFunc

type FilterFunc[T any] func(T) bool

FilterFunc is a predicate for type T

type MapFunc

type MapFunc[T, V any] func(T) V

MapFunc maps the T -> V

Jump to

Keyboard shortcuts

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