Slice - Iterator helpers
This page lists all operations on slice, available in the it lo sub-package.
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!
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 []intfor 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 []intfor item := range filtered {result = append(result, item)}// result contains [1, 3, 5]Subset returns a subset of a sequence from
offsetup tolengthelements.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 []intfor item := range subset {result = append(result, item)}// result contains [3, 4, 5]Slice returns a subset of a sequence from
startup 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 []intfor 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) Ifunc DropByIndex[T any, I ~func(func(T) bool)](collection I, indexes ...int) Ifunc Subset[T any, I ~func(func(T) bool)](collection I, offset, length int) Ifunc 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 []intfor 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) IReplaceAllโ
Returns a sequence with all non-overlapping instances of old replaced by new.
// Basic replacementcollection := func(yield func(int) bool) {yield(1)yield(2)yield(2)yield(3)}replaced := it.ReplaceAll(collection, 2, 99)var result []intfor item := range replaced {result = append(result, item)}// result: [1, 99, 99, 3]// With strings - replacing multiple occurrencesstrings := func(yield func(string) bool) {yield("apple")yield("banana")yield("apple")yield("cherry")yield("apple")}replacedStrings := it.ReplaceAll(strings, "apple", "orange")var fruitResult []stringfor item := range replacedStrings {fruitResult = append(fruitResult, item)}// fruitResult: ["orange", "banana", "orange", "cherry", "orange"]// No matches foundnoMatch := it.ReplaceAll(strings, "grape", "kiwi")var noMatchResult []stringfor item := range noMatch {noMatchResult = append(noMatchResult, item)}// noMatchResult: ["apple", "banana", "apple", "cherry", "apple"] (unchanged)// Empty collectionempty := func(yield func(int) bool) {// no yields}emptyReplaced := it.ReplaceAll(empty, 1, 99)var emptyResult []intfor 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) ICompactโ
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 []intfor 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) IIsSortedโ
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)) // truefmt.Println(it.IsSorted(unsorted)) // falseSimilar:Prototype:func IsSorted[T constraints.Ordered](collection iter.Seq[T]) boolIsSortedByโ
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) boolKeyifyโ
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]intCountValuesByโ
CountValuesBy counts the number of each element returned from transform function.
Is equivalent to chaining Map and CountValues.type Person struct {Name stringAge 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