DEV Community

Discussion on: 7 Easy functional programming techniques in Go

Collapse
 
derek profile image
derek • Edited

I think the situation will dictate the technique.

I definitely think there's a lot of merit to functional programming, and even in usage with go; but perhaps sparingly. Sparingly... meaning maybe it won't be the first thing I reach for when coding go 🤷🏽‍♂️. Due to its type system it's definitely a little bit harder to get more milage out of functional programming like python, javascript, haskell, elm, etc. But you're right perhaps we'll see an evolution of Go and its community and how we write go in the coming years (v2).

I usually regurgitate Uncle Bill's motto "optimize for correctness"

Here's a suggestion to reduce allocations for your mapForEach:

func mapForEach(arr []string, fn func(it string) int) []int {
    out := make([]int, len(arr))
    for i, it := range arr {
        out[i] = len(it)
    }
    return out
}

bench

Collapse
 
deepu105 profile image
Deepu K Sasidharan

Thanks. Nice catch. I wasn't focusing on performance as it was just sample code.

Collapse
 
derek profile image
derek

Gotcha!

I forgot to open my reply with: First off... Thank you for sharing your findings and knowledge!