Golang Slicr (pronounced as slicer) is a collection of generic functions, that makes is easier to operate Golang's slices. You can read all the functions reference down below in this description.
In order to be able to use it, make sure, that you are importing for an Go version >= 1.18, as starting with this version, Go started to support generics, and as mentioned above, these are generic functions.
In order to install this package, you need to use go get:
go get github.com/CristianCurteanu/slicrTable of contents:
- Find
- Contains
- Any
- All
- Filter
- ForEach
- Map
- Reduce
- Pop
- Prepend
- Shift
- Delete
- Extend
- ExtendAt
- ExtendCap
- Deduplicate
- Count
- Partition
- GroupBy
- Chunk
- Zip
- Unzip
- Flatten
- Take
- Drop
- TakeWhile
- DropWhile
- Max
- Min
- Sum
- Product
- Average/Mean
- Copy/Clone
- Equal
Locate the first element that satisfies a predicate.
fruits := []string{"apple", "orange", "banana"}
fruit, found := slicr.Find(fruits, func (f string) bool { return f == "orange" })
fmt.Printf("found: %s", fruit)The generic type of Find will be string, as it will automatically detect by slice type reference. In this case it will return the orange element, and will return the found value as true
In case the predicate will run through each element and will not find the element according to the predicate, it will return zero value of the slice element type, and false to the found flag.
Check if a slice contains a specific element.
fruits := []string{"apple", "orange", "banana"}
found := slicr.Contains(fruits, "orange")
fmt.Printf("found: %s", found)Bear in mind that the generic type of the slice should be comparable, otherwise you need to consider Any, to use a predicate function
Determine if any elements satisfy a predicate
It works the same way as the return found flag of the Find function, but it only returns the bool value, that signals that there is a value that satisfies the predicate function.
fruits := []string{"apple", "orange", "banana"}
found := slicr.Any(fruits, func (f string) bool { return f == "orange" })
fmt.Printf("found: %v", fruit)Check if all elements satisfy a predicate.
fruits := []string{"apple", "orange", "banana"}
noEmptyStrings := slicr.All(fruits, func(f string) bool { return f != "" })
fmt.Printf("any empty string?: %v", !noEmptyStrings)Select elements from a slice that satisfy a given predicate function.
If the predicate returns true, the value will be added to the new slice.
fruits := []string{"apple", "orange", "banana"}
fruits = slicr.Filter(fruits, func (f string) bool { return f == "orange" })
fmt.Printf("selected fruits: %s", fruits)Execute a function for each element in a slice, typically for side effects.
fruits := []string{"apple", "orange", "banana"}
fruits = slicr.ForEach(fruits, func (f string) {
fmt.Println(fruit)
})This could be useful, when an existing predicate function could be applied for each element, and not expecting a result; same as range, but it's a bit cleaner.
Apply a function to each element of a slice, producing a new slice with the results.
type Fruit struct {
Name string
}
fruits := []string{"apple", "orange", "banana"}
fruitsEntities := slicr.Map(fruits, func (f string) Fruit { return Fruit{Name: f} })
fmt.Printf("mapped fruits: %+v", fruitsEntities)The predicate acts as mapping function.
Aggregate elements of a slice into a single value using an accumulator function.
fruits := []string{"apple", "orange", "banana"}
list = slicr.Reduce(fruits, func (res, f string) string {
return fmt.Sprintf("%s, %s", res, f)
})
fmt.Printf("reduced fruits: %s", list) // reduced fruits: apple, orange, bananaThis will fold the initial string slice, into a single string, by joining all elements according to the reducer function.
Pops the last element of the slice, and reduces the slice by last popped element
fruits := []string{"apple", "orange", "banana"}
fruit, fruits := slicr.Pop(fruits)
fmt.Printf("popped: %q\n", fruit) // popped: "banana"
fmt.Printf("fruits: %+v", fruits) // fruits: [apple orange]Adds a new element at the beginning of the slice
fruits := []string{"apple", "orange", "banana"}
fruits = slicr.Prepend(fruits, "mango", "pineapple")
fmt.Printf("fruits: %+v", fruits)Pops an element at the beginning of the slice
fruits := []string{"apple", "orange", "banana"}
fruit, fruits := slicr.Shift(fruits)
fmt.Printf("fruit: %+v\n", fruit) // fruit: apple
fmt.Printf("fruits: %+v", fruits) // fruits: [orange banana]Delete element at the given index
fruits := []string{"apple", "orange", "banana"}
fruits = slicr.Delete(fruits, 1)
fmt.Printf("fruits: %+v", fruits) // fruits: [apple banana]Extends the size of a given slice.
fruits := []string{"apple", "orange", "banana"}
fmt.Printf("fruits.size: %+v\n", len(fruits)) // fruits.size: 3
fruits = slicr.Extend(fruits, 1)
fmt.Printf("fruits.size: %+v", len(fruits)) // fruits.size: 4Extends the size of slice by n elements at position i
fruits := []string{"apple", "orange", "banana"}
fmt.Printf("fruits.size: %+v\n", len(fruits)) // fruits.size: 3
fruits = slicr.ExtendAt(fruits, 2, 1)
fmt.Printf("fruits.size: %+v\n", len(fruits)) // fruits.size: 5
fmt.Printf("fruits: %+v", fruits) // fruits: [apple orange banana]Extends the capacity of a given slice by n elements
fruits := []string{"apple", "orange", "banana"}
fmt.Printf("fruits.cap: %+v\n", cap(fruits)) // fruits.cap: 3
fruits = slicr.ExtendCap(fruits, 5)
fmt.Printf("fruits.cap: %+v\n", cap(fruits)) // fruits.cap: 8
fmt.Printf("fruits: %+v", fruits) // fruits: [apple orange banana]Removes all duplicate elements in a slice
fruits := []string{"apple", "orange", "banana", "orange"}
fruits = Deduplicate(fruits)
fmt.Printf("fruits: %+v", fruits) // fruits: [apple banana orange]Count the number of elements that satisfy a predicate
fruits := []string{"apple", "orange", "banana", "orange"}
count := Count(fruits, func(f string) bool {
return f == "orange"
})
fmt.Printf("oranges: %+v", count) // oranges: 2Split a slice into two slices based on a predicate.
fruits := []string{"apple", "orange", "banana", "orange"}
oranges, fruits := Partition(fruits, func(f string) bool {
return f == "orange"
})
fmt.Printf("oranges: %+v\n", oranges) // oranges: [orange orange]
fmt.Printf("fruits: %+v", fruits) // fruits: [apple banana]Group elements of a slice based on a key selector function.
fruits := []string{"apple", "orange", "banana", "orange"}
groupedFruits := GroupBy(fruits, func(f string) (string, string) {
return string(f[len([]byte(f))-1]), f // This will group by last char of strings, which will be `e` and `a` groups
})
fmt.Printf("fruits: %+v", groupedFruits) // fruits: [[apple orange orange] [banana]]Split a slice into multiple slices of a specified size.
fruits := []string{"apple", "orange", "banana", "orange"}
groupedFruits := Chunk(fruits, 3)
fmt.Printf("fruits: %+v", groupedFruits) // fruits: [[apple orange banana] [orange]]Combine two slices into one slice of tuples.
Separate a slice of tuples into two slices.
Flatten a slice of slices into a single slice.
Take the first N elements from a slice.
Skip the first N elements of a slice.
Take elements from the slice while a predicate is true.
Skip elements in the slice while a predicate is true.
Find the maximum element in a slice (requires ordering).
Find the minimum element in a slice (requires ordering).
Calculate the sum of elements (numeric slices).
Calculate the product of elements (numeric slices).
Compute the average value of elements (numeric slices).
Create a shallow copy of a slice.
Check if two slices are equal in content and order.