hashicorp / go-secure-stdlib

Mozilla Public License 2.0
64 stars 24 forks source link

Functional will contain functional programming patterns made possible by Go generics. #55

Closed sgmiller closed 1 year ago

sgmiller commented 1 year ago

This PR adds the lazy pattern, which allows Producers (func() V), Functions (func(A) V), BiFunctions (func(A,B),V) and a version of each which also returns an error to be wrapped in logic which evaluates the underlying function such that when initially called the function calculates the result. All subsequent calls return the results of the first again.

This can be used to lazily compute a value or reach out to an expensive resource once, yet have the function be called throughout code without worrying about double-calculation or difficult coordinary across function boundaries.

swenson commented 1 year ago

In light of @johanbrandhorst's comment, I retract my review for now :)

jimlambrt commented 1 year ago

clarity is paramount and I'd prefer this was kept private to repositories where it is widely employed and understood, if at all. These sort of patterns are not widely used within Go, as far as I know. Even a year on from generics being released these patterns have not emerged as a common way to do things.

Just here to give a strong :+1 to @johanbrandhorst comment

sgmiller commented 1 year ago

Will pull into the using repo for now.