option
option copied to clipboard
Typed Return Type for `flatmap` contradicts documentation
The documentation for flatmap
states: "This is different than Option.map() because the result of the callback isn’t wrapped in a new Option." This test verifies that behavior:
def test_flatmap_some():
assert_that(option.Some(4).flatmap(lambda x: x + 1), equal_to(5))
However, the return type of the function is specified to be Option[U]
.
What is the expected behavior flatmap
?
I observed a similar discrepancy for option.Result.flatmap
.
Hi, the documentation might be confusing. The intended purpose for flatmap
is to have the call back you provide return an Option/Result
, but for the final value be flattened into a single Option/Result
. This is useful for chaining multiple computations that can fail/produce NONE
with fail fast semantics.
Suggestions for new wording on the doc is welcome.
For example,
option.Some(5).map(lambda x: option.Some(x + 1)) == Some(Some(6))
but
option.Some(5).flatmap(lambda x: option.Some(x + 1)) == Some(6)
Thank you for the response.
Unfortunately, I’m swamped dealing with family issues and cannot focus on this issue right now. Hopefully, I will remember your response when I’m “unswamped.”
Have a great day!
On Mar 30, 2022, at 3:04 PM, MaT1g3R @.***> wrote:
Hi, the documentation might be confusing. The intended purpose for flatmap is to have the call back you provide return an Option/Result, but for the final value be flattened into a single Option/Result. This is useful for chaining multiple computation that can fail/produce NONE with fail fast semantics.
Suggestions for new wording on the doc is welcome.
For example,
option.Some(5).map(lambda x: option.Some(x + 1)) == Some(Some(6)) but
option.Some(5).flatmap(lambda x: option.Some(x + 1)) == Some(6) — Reply to this email directly, view it on GitHub https://github.com/MaT1g3R/option/issues/6#issuecomment-1083571523, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAERLXXHGAFE2FN5HENB6ILVCSXT3ANCNFSM43L2GVMQ. You are receiving this because you authored the thread.
@mrwizard82d1 thanks, wish you the best on your family issues
Thank you very much.
On Mar 30, 2022, at 3:11 PM, MaT1g3R @.***> wrote:
@mrwizard82d1 https://github.com/mrwizard82d1 thanks, wish you the best on your family issues
— Reply to this email directly, view it on GitHub https://github.com/MaT1g3R/option/issues/6#issuecomment-1083577952, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAERLXVPWLYUYA2QV7T74JLVCSYO7ANCNFSM43L2GVMQ. You are receiving this because you were mentioned.