Skip to content

Shortcut for flatMap on possibly falsy data (flatMapFromFalsy) #217

Description

@tvedtorama

I find myself writing this all the time:

maybe.flatMap(x => Maybe.fromFalsy(x.value)).

It would be very useful to have an

maybe.flatMapFromFalsy(x => x.value)

or

maybe.flatMap(x => x.value, Maybe.fromFalsy)
// Which can also be used with other constructors:
maybe.flatMap(x => x.value, Maybe.fromNull)

Does this make sense, or am I perhaps using the library a bit wrong?

Activity

  1. ulfryk commented on Aug 16, 2019

    @ulfryk
    Member

    @tvedtorama - your request is for sure a thing. I also find myself composing flat map with fromNull

    Without need to access nested properties solution would be using the constructor as a passed lambda:

    maybe.flatMap(Maybe.fromFalsy)

    but for mentioned above case there are no tools in monet. You can always use compose from any other functional library:

    maybe.flatMap(compose(Maybe.fromFalsy, x => x.value))

    But as this seems to be quite a common use case, I'd think about how it can be enhanced in a nicest way :)

  2. ulfryk commented on Nov 19, 2024

    @ulfryk
    Member

    @tvedtorama please see #258

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions