Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- )
- func apply(arr []float64, callback func(float64) float64) []float64 {
- out := make([]float64, len(arr))
- for i, e := range arr {
- out[i] = callback(e)
- }
- return out
- }
- func main() {
- arr := []float64{1, 2, 3, 4, 5}
- // Square
- sqr := apply(arr, func(n float64) float64 { return n * n })
- fmt.Println(sqr)
- // Cubic
- cub := apply(arr, func(n float64) float64 { return n * n * n })
- fmt.Println(cub)
- // Inverse
- inv := apply(arr, func(n float64) float64 { return 1.0 / n })
- fmt.Println(inv)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement