Back to Lo

It Maptoseq

docs/data/it-maptoseq.md

1.53.0931 B
Original Source

Transforms a map into a sequence by applying a transform function to each key-value pair. The transform function determines what values are yielded in the output sequence.

go
m := map[string]int{
    "apple":  3,
    "banana": 5,
    "cherry": 2,
}
result := it.MapToSeq(m, func(key string, value int) string {
    return fmt.Sprintf("%s:%d", key, value)
})
// iter.Seq[string] yielding "apple:3", "banana:5", "cherry:2"

numberMap := map[int]string{1: "one", 2: "two", 3: "three"}
result = it.MapToSeq(numberMap, func(key int, value string) int {
    return key * len(value)
})
// iter.Seq[int] yielding 3, 6, 15 (1*3, 2*3, 3*5)

personMap := map[string]int{"alice": 25, "bob": 30}
type Person struct {
    Name string
    Age  int
}
result = it.MapToSeq(personMap, func(name string, age int) Person {
    return Person{Name: name, Age: age}
})
// iter.Seq[Person] yielding {Name: "alice", Age: 25}, {Name: "bob", Age: 30}