Files
lo/docs/data/mutable-map.md
T
Nathan Baulch e7386d9246 lint: fix inconsistent callback function parameter names (#730)
* Fix linting

* Use is.ElementsMatch

This will ignore the ordering of the final intersection. Especially
important when checking old versions of go that do not guarantee an order
when iterating through maps.

* lint: fix inconsistent callback function parameter names

* lint: rename "iteratee" to "transform" for *Map helpers

* lint: rename "project" parameters to "transform"

* lint: rename "cb" parameters to "callback"

* lint: rename "iteratee" to "callback" for ForEach helpers

---------

Co-authored-by: Franky W. <frankywahl@users.noreply.github.com>
Co-authored-by: Samuel Berthe <dev@samuel-berthe.fr>
2025-11-06 18:05:11 +01:00

63 lines
1.4 KiB
Markdown

---
name: Map
slug: map
sourceRef: mutable/slice.go#L41
category: mutable
subCategory: slice
playUrl: https://go.dev/play/p/0jY3Z0B7O_5
variantHelpers:
- "mutable#slice#map"
- "mutable#slice#mapi"
similarHelpers:
- core#slice#map
- core#slice#mapkeys
- core#slice#mapvalues
- parallel#slice#map
position: 10
signatures:
- "func Map[T any, Slice ~[]T](collection Slice, transform func(item T) T)"
- "func MapI[T any, Slice ~[]T](collection Slice, transform func(item T, index int) T)"
---
Transforms each element in the slice by applying the transform 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`.
```go
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:
```go
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):
```go
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"}
```