Created
August 20, 2017 07:30
-
-
Save ajkavanagh/81a8d0140cb2cfa463d7b76b84e8f896 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
// Accumulate takes a []string, applies func f to each | |
// element and returns the resulting []string. | |
func Accumulate(s []string, f func(string) string) []string { | |
for i := range s { | |
s[i] = f(s[i]) | |
} | |
return s | |
} | |
func change(x string) string { | |
x = x + "one" | |
return x | |
} | |
func main() { | |
a := []string{"Hello", "there"} | |
fmt.Printf("Strings: %s\n", a) | |
b := Accumulate(a, change) | |
fmt.Printf("a strings: %s\n", a) | |
fmt.Printf("b strings: %s\n", b) | |
} | |
Strings: [Hello there] | |
a strings: [Helloone thereone] | |
b strings: [Helloone thereone] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment