Back to Lo

It Replace

docs/data/it-replace.md

1.53.0395 B
Original Source

Replace returns a sequence with the first n non-overlapping instances of old replaced by new.

go
collection := func(yield func(int) bool) {
    yield(1)
    yield(2)
    yield(2)
    yield(3)
    yield(2)
    yield(4)
}

replaced := it.Replace(collection, 2, 99, 2)
var result []int
for item := range replaced {
    result = append(result, item)
}
// result contains [1, 99, 99, 3, 2, 4]