Providing these methods as a separate package also matches Go's primitives and standard library:
The string type don't have methods, but there we have the strings package.
The []byte type don't have methods, but there we have the bytes package.
The io.Reader defines a single method, and all manipulations of a reader is done on packages io and ioutil.
This package includes:
options.Map
options.FlatMap
options.Match
options.FlatMatch
The FlatMatch function is to replace this pattern:
var thing ExpensiveThing
if opt.IsPresent() {
thing = NewExpensiveThingFrom(opt.MustGet())
} else {
thing = NewExpensiveThing()
}
// Can now be done like this:
thing := options.FlatMatch(opt, NewExpensiveThingFrom, NewExpensiveThing)
I would have this be the behavior of Match and return an Option when wanted, but since there is already a method that use the two return values I decided to have the same signature for the function with same name and create a new function for this pattern. I called it FlatMatch since it allows the same use case as FlatMap, where the given functions return an Option and the result is that same Option.
If you agree with this idea I can send a results package right away.
As stated on the package doc:
This package includes:
options.Map
options.FlatMap
options.Match
options.FlatMatch
The
FlatMatch
function is to replace this pattern:I would have this be the behavior of
Match
and return anOption
when wanted, but since there is already a method that use the two return values I decided to have the same signature for the function with same name and create a new function for this pattern. I called itFlatMatch
since it allows the same use case asFlatMap
, where the given functions return anOption
and the result is that sameOption
.If you agree with this idea I can send a
results
package right away.