Back to Lo

It Map

docs/data/it-map.md

1.53.0861 B
Original Source

Transforms a sequence to another type by applying a transform function to each element.

Examples:

go
seq := func(yield func(int) bool) {
    _ = yield(1)
    _ = yield(2)
    _ = yield(3)
}
mapped := it.Map(seq, func(x int) string {
    return fmt.Sprintf("item-%d", x)
})
var result []string
for v := range mapped {
    result = append(result, v)
}
// result contains "item-1", "item-2", "item-3"

MapI

Transforms a sequence to another type by applying a transform function to each element and its index.

go
seq := func(yield func(int) bool) {
    _ = yield(10)
    _ = yield(20)
    _ = yield(30)
}
mapped := it.MapI(seq, func(x int, index int) string {
    return fmt.Sprintf("item-%d-%d", x, index)
})
var result []string
for v := range mapped {
    result = append(result, v)
}
// result contains "item-10-0", "item-20-1", "item-30-2"