8000 Cannot compose Either · Issue #37 · jasondelaat/pymonad · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Cannot compose Either #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
benner opened this issue May 6, 2024 · 1 comment
Open

Cannot compose Either #37

benner opened this issue May 6, 2024 · 1 comment

Comments

@benner
Copy link
benner commented May 6, 2024

Playing with library and and got confused.

Code:

from pymonad.either import Left, Right
from pymonad.reader import Compose

def check(x):
    if x < 0:
        return Left("cannot be negative")

    return Right(x)

def add5(x):
    return Right(x + 5)

def mul3(x):
    return Right(x * 2)

right_path=check(5).then(check).then(add5).then(mul3)
print(right_path)

left_path=check(-5).then(check).then(add5).then(mul3)
print(left_path)

f=Compose(check).then(add5).then(mul3)
print(f(-5))

Firs two prints works as expected:

Right 20
Left cannot be negative

but last one gives error:

..
.. line 11, in add5
    return Right(x + 5)
TypeError: unsupported operand type(s) for +: 'Either' and 'int'

Is this expected?

@vreuter
Copy link
vreuter commented Jul 18, 2024

@benner IIRC it's correct / expected, as then is designed to be "smarter" in a sense (at least, more functionally flexible), while Compose is meant to do pure function composition (i.e., not unwrapping the result of a previous monadic computation before passing it to the next chained function).

At least, that's what the then function's documentation on the either page, in conjunction with the description of Compose in the reader doc suggest to me. I'm new to the library, though, so take w/ grain of salt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
0