go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/iter/apply.go (about) 1 /* 2 3 Copyright (c) 2023 - Present. Will Charczuk. All rights reserved. 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository. 5 6 */ 7 8 package iter 9 10 // Apply maps a given function against a given input values yielding 11 // potentially a new list of a different type but of the same length. 12 func Apply[T, V any](values []T, fn func(T) V) (output []V) { 13 output = make([]V, len(values)) 14 for index, value := range values { 15 output[index] = fn(value) 16 } 17 return output 18 } 19 20 // ApplyError maps a given function against a given input values yielding 21 // potentially a new list of a different type but of the same length. 22 func ApplyError[T, V any](values []T, fn func(T) (V, error)) (output []V, err error) { 23 output = make([]V, len(values)) 24 for index, value := range values { 25 output[index], err = fn(value) 26 if err != nil { 27 return 28 } 29 } 30 return 31 }