mirror of
https://github.com/samber/lo.git
synced 2026-04-22 23:47:11 +08:00
e7386d9246
* 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>
1.4 KiB
1.4 KiB
name, slug, sourceRef, category, subCategory, playUrl, variantHelpers, similarHelpers, position, signatures
| name | slug | sourceRef | category | subCategory | playUrl | variantHelpers | similarHelpers | position | signatures | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Map | map | mutable/slice.go#L41 | mutable | slice | https://go.dev/play/p/0jY3Z0B7O_5 |
|
|
10 |
|
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.
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"}