8000 How to unwrap Monads for return statements that expect a value? · Issue #499 · evilsoft/crocks · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
How to unwrap Monads for return statements that expect a value? #499
Open
@JustinHoyt

Description

@JustinHoyt

Is your feature request related to a problem? Please describe.
In my use case I would like to use functional programming techniques, using ramda for a functional library and Crocks for an algebraic data structure library to write code in a not fully functional codebase. No one else on my team has any familiarity with Functional Programming and I'm new to it myself. I want to be able to pull values out of monads so I can return normal values in a codebase that's not functional in nature and slowly add more functional elements. I'm typically going to be using Either, IO, and Maybe monads to write my code, then extract the final result out of the resulting monad so I can return the value to a function that is not made to accept monads yet.

Describe the solution you'd like
Folktale has a function called getOrElse which will return a value or an undefined/error string. This is super useful and allows me to write functionally in an environment that does not expect to handle monads. For example, I can make some Results/Either monads, chain them together and then carefully unwrap the value for possible return. I get some functioal benefits with a small amount of impurity at the end of my function. Does Crocks have something similar or is there another way I can unwrap Either, IO, or Maybe with an internal function?

Describe alternatives for how you do this now
My current workaround is just to use Folktale, but the maintainer has mentioned that they don't have the bandwidth to maintain the project anymore. Folktale also has a limited amount of monads for me to use.

Code

const simpleFunction = (a) => {
    myMaybe = Maybe.of(a);

    // some random transformations on myMaybe

    // this will fall back to the second case if the maybe is empty
    return myMaybe.getOrElse() || doSomethingElseOnError();
}

Additional context
I also made a stackoverflow with this question, but could not tag it properly because I don't have enough reputation to add a Crocks tag.

Thanks in advance for your advice or feedback!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0