dbrattli / Expression

Pragmatic functional programming for Python inspired by F#
https://expression.readthedocs.io
MIT License
421 stars 30 forks source link

Issue on page /tutorial/effects.html #96

Closed yurigba closed 1 year ago

yurigba commented 1 year ago

I am trying to reproduce the following code block from this tutorial:

from expression import effect
from expression.core import option, Option, Some, Nothing

def divide(a: float, divisor: float) -> Option[int]:
    try:
        return Some(a/divisor)
    except ZeroDivisionError:
        return Nothing

@effect.option
def comp(x):
    result = yield from divide(42, x)
    result += 32
    print(f"The result is {result}")
    return result

comp(42)

It returns the following on jupyter lab:

TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_477668/4077434771.py in <cell line: 11>()
     10 
     11 @effect.option
---> 12 def comp(x):
     13     result = yield from divide(42, x)
     14     result += 32

TypeError: OptionBuilder() takes no arguments

I couldn't make this code to work. Any suggestions?

yurigba commented 1 year ago

The Python version is 3.10.6

dbrattli commented 1 year ago

Hi, thanks for flagging this issue. The correct code should be:

from expression import effect, Option, Some, Nothing

def divide(a: float, divisor: float) -> Option[float]:
    try:
        return Some(a / divisor)
    except ZeroDivisionError:
        return Nothing

@effect.option[float]()
def comp(x: float):
    result: float = yield from divide(42, x)
    result += 32
    print(f"The result is {result}")
    return result

comp(42)
yurigba commented 1 year ago

It worked. Thank you.