Skip to content

Commit

Permalink
Intermediate changes
Browse files Browse the repository at this point in the history
commit_hash:84203a3b169a31d19c11a4e1ea2528faa988d851
  • Loading branch information
robot-piglet committed Nov 29, 2024
1 parent 1ae7ed6 commit af92f3c
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion library/go/slices/map.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
package slices

// MapP applies given function to every pointer to element of slice
func MapP[S ~[]T, T, M any](s S, fn func(*T) M) []M {
if s == nil {
return []M(nil)
}
if len(s) == 0 {
return make([]M, 0)
}
res := make([]M, len(s))
for i := range s {
res[i] = fn(&s[i])
}
return res
}

// Map applies given function to every value of slice
func Map[S ~[]T, T, M any](s S, fn func(T) M) []M {
if s == nil {
Expand All @@ -15,7 +30,7 @@ func Map[S ~[]T, T, M any](s S, fn func(T) M) []M {
return res
}

// Map applies given function to every value of slice
// MapE applies given function to every value of slice and return slice or first error
func MapE[S ~[]T, T, M any](s S, fn func(T) (M, error)) ([]M, error) {
if s == nil {
return []M(nil), nil
Expand Down

0 comments on commit af92f3c

Please sign in to comment.