-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.go
More file actions
54 lines (46 loc) · 1.83 KB
/
Copy pathmap.go
File metadata and controls
54 lines (46 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package godash
// MapFn is a function type that transforms elements of a collection.
//
// Parameters:
// - element T: The current element being processed.
// - index int: The zero-based index of the current element in the collection.
// - collection []T: The entire collection being iterated over.
//
// Returns:
// - U: The transformed value.
// - error: An error if the transformation fails.
type MapFn[T, U any] func(element T, index int, collection []T) (U, error)
// MapFnNE is a no-error variant of MapFn where the mapper does not return an error.
type MapFnNE[T, U any] func(element T, index int, collection []T) U
// Map transforms each element of a collection using the provided function
// and returns a new slice containing the transformed elements.
//
// Parameters:
// - collection: The input slice of elements to transform.
// - mapFunction: The transformation function to apply to each element.
//
// Returns:
// - A new slice containing the transformed elements.
// - An error if any transformation fails. The partially populated result
// is returned along with the error.
func Map[T, U any](collection []T, mapFunction MapFn[T, U]) ([]U, error) {
mapped := make([]U, len(collection))
for index, element := range collection {
m, err := mapFunction(element, index, collection)
if err != nil {
return mapped, err
}
mapped[index] = m
}
return mapped, nil
}
// MapNE is a convenience wrapper for Map that accepts a no-error mapper and
// returns only the resulting slice. Any error from the underlying Map is ignored
// because the provided mapper cannot return an error.
func MapNE[T, U any](collection []T, mapFunction MapFnNE[T, U]) []U {
adapted := func(element T, index int, collection []T) (U, error) {
return mapFunction(element, index, collection), nil
}
r, _ := Map(collection, adapted)
return r
}