This documentation is still new and evolving. If you spot any mistakes, unclear explanations, or missing details, please open an issue.
Your feedback helps us improve!
Iterator - Map helpersโ
This page lists all operations on slice, available in the it
lo sub-package.
DropLastโ
DropLast drops n elements from the end of a sequence.
collection := func(yield func(int) bool) {
yield(1)
yield(2)
yield(3)
yield(4)
yield(5)
}
filtered := it.DropLast(collection, 2)
var result []int
for item := range filtered {
result = append(result, item)
}
// result contains [1, 2, 3]DropByIndex drops elements from a sequence by the index.
collection := func(yield func(int) bool) {
yield(1)
yield(2)
yield(3)
yield(4)
yield(5)
}
filtered := it.DropByIndex(collection, 1, 3)
var result []int
for item := range filtered {
result = append(result, item)
}
// result contains [1, 3, 5]Subset returns a subset of a sequence from
offset
up tolength
elements.collection := func(yield func(int) bool) {
yield(1)
yield(2)
yield(3)
yield(4)
yield(5)
yield(6)
}
subset := it.Subset(collection, 2, 3)
var result []int
for item := range subset {
result = append(result, item)
}
// result contains [3, 4, 5]Slice returns a subset of a sequence from
start
up to, but not includingend
.collection := func(yield func(int) bool) {
yield(1)
yield(2)
yield(3)
yield(4)
yield(5)
}
sliced := it.Slice(collection, 1, 4)
var result []int
for item := range sliced {
result = append(result, item)
}
// result contains [2, 3, 4]Variant:Prototypes:func DropLast[T any, I ~func(func(T) bool)](collection I, n int) I
func DropByIndex[T any, I ~func(func(T) bool)](collection I, indexes ...int) I
func Subset[T any, I ~func(func(T) bool)](collection I, offset, length int) I
func Slice[T any, I ~func(func(T) bool)](collection I, start, end int) IReplaceโ
Replace returns a sequence with the first n non-overlapping instances of old replaced by new.
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]Similar:Prototype:func Replace[T comparable, I ~func(func(T) bool)](collection I, old, nEw T, n int) I
ReplaceAllโ
Returns a sequence with all non-overlapping instances of old replaced by new.
// Basic replacement
collection := func(yield func(int) bool) {
yield(1)
yield(2)
yield(2)
yield(3)
}
replaced := it.ReplaceAll(collection, 2, 99)
var result []int
for item := range replaced {
result = append(result, item)
}
// result: [1, 99, 99, 3]
// With strings - replacing multiple occurrences
strings := func(yield func(string) bool) {
yield("apple")
yield("banana")
yield("apple")
yield("cherry")
yield("apple")
}
replacedStrings := it.ReplaceAll(strings, "apple", "orange")
var fruitResult []string
for item := range replacedStrings {
fruitResult = append(fruitResult, item)
}
// fruitResult: ["orange", "banana", "orange", "cherry", "orange"]
// No matches found
noMatch := it.ReplaceAll(strings, "grape", "kiwi")
var noMatchResult []string
for item := range noMatch {
noMatchResult = append(noMatchResult, item)
}
// noMatchResult: ["apple", "banana", "apple", "cherry", "apple"] (unchanged)
// Empty collection
empty := func(yield func(int) bool) {
// no yields
}
emptyReplaced := it.ReplaceAll(empty, 1, 99)
var emptyResult []int
for item := range emptyReplaced {
emptyResult = append(emptyResult, item)
}
// emptyResult: [] (empty sequence)Similar:Prototype:func ReplaceAll[T comparable, I ~func(func(T) bool)](collection I, old, nEw T) I
Compactโ
Compact returns a sequence of all non-zero elements.
collection := func(yield func(int) bool) {
yield(0)
yield(1)
yield(0)
yield(2)
yield(3)
yield(0)
}
compacted := it.Compact(collection)
var result []int
for item := range compacted {
result = append(result, item)
}
// result contains [1, 2, 3]Similar:Prototype:func Compact[T comparable, I ~func(func(T) bool)](collection I) I
IsSortedโ
IsSorted checks if a sequence is sorted.
sorted := func(yield func(int) bool) {
yield(1)
yield(2)
yield(3)
yield(4)
}
unsorted := func(yield func(int) bool) {
yield(1)
yield(3)
yield(2)
yield(4)
}
fmt.Println(it.IsSorted(sorted)) // true
fmt.Println(it.IsSorted(unsorted)) // falseSimilar:Prototype:func IsSorted[T constraints.Ordered](collection iter.Seq[T]) bool
IsSortedByโ
IsSortedBy checks if a sequence is sorted by transform.
collection := func(yield func(string) bool) {
yield("apple")
yield("banana")
yield("cherry")
}
sortedByLength := it.IsSortedBy(collection, func(s string) int {
return len(s)
})
// true (5, 6, 6 is sorted)Similar:Prototype:func IsSortedBy[T any, K constraints.Ordered](collection iter.Seq[T], transform func(item T) K) bool
Keyifyโ
Keyify returns a map with each unique element of the sequence as a key.
collection := func(yield func(int) bool) {
yield(1)
yield(2)
yield(1)
yield(3)
yield(2)
}
keyMap := it.Keyify(collection)
// keyMap contains {1: {}, 2: {}, 3: {}}Similar:Prototype:func Keyify[T comparable](collection iter.Seq[T]) map[T]struct{}
CountValuesโ
CountValues counts the number of each element in the collection.
collection := func(yield func(string) bool) {
yield("apple")
yield("banana")
yield("apple")
yield("cherry")
yield("banana")
yield("apple")
}
counts := it.CountValues(collection)
// counts contains {"apple": 3, "banana": 2, "cherry": 1}Similar:Prototype:func CountValues[T comparable](collection iter.Seq[T]) map[T]int
CountValuesByโ
CountValuesBy counts the number of each element returned from transform function.
Is equivalent to chaining Map and CountValues.type Person struct {
Name string
Age int
}
collection := func(yield func(Person) bool) {
yield(Person{"Alice", 25})
yield(Person{"Bob", 30})
yield(Person{"Charlie", 25})
yield(Person{"Diana", 30})
}
countsByAge := it.CountValuesBy(collection, func(p Person) int {
return p.Age
})
// countsByAge contains {25: 2, 30: 2}Similar:Prototype:func CountValuesBy[T any, U comparable](collection iter.Seq[T], transform func(item T) U) map[U]int