| (Unfortunately, formatting is lost) a) With a more complex type (I don't see how this would affect one more than the other) Functional would be: foos = map(foos, func(f Foo) Foo {
return Foo{f.A * f.A, f.B * f.B}
}) Iterative is: for i, f := range foos {
foos[i] = Foo{f.A * f.A, f.B * f.B}
} b) Composing various operations. Composing filter + map: Functional would be: x := filter(map(numbers, func(n int) int { return n * n }), func(n int) bool { return n % 2 == 0 }) Iterative is: x := make([]int, 0);
for _, n := range numbers {
sq := n * n;
if sq % 2 == 0 {
x = append(x, sq)
}
} c) Swapping the container type is just a matter of swapping "_, n := range numbers". |
It does because the discussion is about generics. The fact that you're writing out types for one and not the other makes your comparisons a bit disingenuous, though I understand if Go isn't very good at functional programming.
b) Composing various operations. Composing filter + map:
As you keep building them up the imperative style will have more code overhead and less efficiency.
> c) Swapping the container type is just a matter of swapping "_, n := range numbers".
This isn't true since the functional one could easily swap out to use an infinite data source whereas the imperative will be constrained to the arrays you're already using.