Skip to main content
Help improve this documentation

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!

Mutable - Slice helpersโ€‹

This page lists all operations on slices, available in the mutable lo sub-package.

  • Modifies the input slice in place, keeping only the elements for which the predicate returns true. Order is preserved. Returns the shortened slice view backed by the original array.

    Variants: FilterI accepts an index-aware predicate (item T, index int) bool.

    import lom "github.com/samber/lo/mutable"

    list := []int{1, 2, 3, 4}
    kept := lom.Filter(list, func(x int) bool {
    return x%2 == 0
    })

    // kept -> []int{2, 4}
    // list is modified in place (backed by same array)

    Another example with a struct type:

    type User struct { Name string; Active bool }

    users := []User{{"Alex", true}, {"Bob", false}, {"Carol", true}}
    active := lom.Filter(users, func(u User) bool {
    return u.Active
    })

    // active -> []User{{"Alex", true}, {"Carol", true}}
    // users underlying storage is reused

    Index-aware variant (FilterI):

    // keep even-indexed items whose length >= 2
    list2 := []string{"a", "bb", "ccc", "dddd"}
    kept2 := lom.FilterI(list2, func(s string, i int) bool {
    return i%2 == 0 && len(s) >= 2
    })
    // kept2 -> []string{"ccc"}

    nums := []int{10, 11, 12, 13, 14}
    evenPos := lom.FilterI(nums, func(_ int, i int) bool {
    return i%2 == 0
    })
    // evenPos -> []int{10, 12, 14}
    Variant:
    Prototypes:
    func Filter[T any, Slice ~[]T](collection Slice, predicate func(item T) bool) Slice
    func FilterI[T any, Slice ~[]T](collection Slice, predicate func(item T, index int) bool) Slice
  • Transforms each element in the slice by applying the mapper function in place. The length remains unchanged; values are overwritten in the same backing array.

    Variants: MapI accepts an index-aware mapper (item T, index int) T.

    import lom "github.com/samber/lo/mutable"

    list := []int{1, 2, 3, 4}
    lom.Map(list, func(x int) int {
    return x * 2
    })
    // list -> []int{2, 4, 6, 8}

    Mapping strings:

    words := []string{"go", "LoDash", "lo"}
    lom.Map(words, func(s string) string {
    return strings.ToUpper(s)
    })
    // words -> []string{"GO", "LODASH", "LO"}

    Index-aware variant (MapI):

    nums := []int{10, 11, 12}
    // add index to each number
    lom.MapI(nums, func(x int, i int) int {
    return x + i
    })
    // nums -> []int{10, 12, 14}

    vals := []string{"a", "b", "c", "d"}
    lom.MapI(vals, func(s string, i int) string {
    if i%2 == 0 { return s + "!" }
    return s
    })
    // vals -> []string{"a!", "b", "c!", "d"}
    Variant:
    Prototypes:
    func Map[T any, Slice ~[]T](collection Slice, fn func(item T) T)
    func MapI[T any, Slice ~[]T](collection Slice, fn func(item T, index int) T)
  • Shuffles the slice in place using the Fisherโ€“Yates algorithm. The operation mutates the original slice order.

    import lom "github.com/samber/lo/mutable"

    list := []int{0, 1, 2, 3, 4, 5}
    lom.Shuffle(list)
    // list order is randomized, e.g., []int{1, 4, 0, 3, 5, 2}

    With strings:

    names := []string{"alice", "bob", "carol"}
    lom.Shuffle(names)
    // names order is randomized
    Prototype:
    func Shuffle[T any, Slice ~[]T](collection Slice)
  • Reverses the slice in place so the first element becomes the last, the second becomes the second-to-last, and so on.

    import lom "github.com/samber/lo/mutable"

    list := []int{0, 1, 2, 3, 4, 5}
    lom.Reverse(list)
    // list -> []int{5, 4, 3, 2, 1, 0}

    With custom types:

    type Point struct{ X, Y int }
    pts := []Point{{0,0}, {1,1}, {2,2}}
    lom.Reverse(pts)
    // pts -> []Point{{2,2}, {1,1}, {0,0}}
    Prototype:
    func Reverse[T any, Slice ~[]T](collection Slice)
  • Fills all elements of a slice with the specified initial value. The operation modifies the slice in place.

    slice := make([]int, 5)
    lo.Fill(slice, 42)
    // []int{42, 42, 42, 42, 42}

    slice = make([]string, 3)
    lo.Fill(slice, "default")
    // []string{"default", "default", "default"}

    slice = make([]bool, 4)
    lo.Fill(slice, true)
    // []bool{true, true, true, true}

    slice = []int{1, 2, 3, 4, 5}
    lo.Fill(slice, 0)
    // []int{0, 0, 0, 0, 0}
    Prototype:
    func Fill[T any, Slice ~[]T](collection Slice, initial T)