package fp // Fold applies a function to each element in the slice and accumulates the result. // It takes a slice of type T, an initial value of type A, and a function that combines the accumulator value with each element of the slice. // The function returns the final accumulated value. func Fold[A any, T any](s []T, a A, f func(A, T) A) A { for i := range s { a = f(a, s[i]) } return a } // Map applies the function f to each element of the slice s and returns a new slice // containing the results. // // The returned slice will contain elements of type M, which is the result // type of the function f. // // Example usage: // // numbers := []int{1, 2, 3, 4, 5} // doubled := Map(numbers, func(n int) int { // return n * 2 // }) // // doubled is now []int{2, 4, 6, 8, 10} // // names := []string{"Alice", "Bob", "Charlie"} // lengths := Map(names, func(name string) int { // return len(name) // }) // // lengths is now []int{5, 3, 7} // // Note: The input slice s is not modified by this function. func Map[T, M any](s []T, f func(T) M) []M { res := []M{} for i := range s { res = append(res, f(s[i])) } return res }