ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Maybe
    python/returns 2022. 9. 4. 21:08

     

    https://returns.readthedocs.io/en/latest/pages/maybe.html

    (scala 의 Option 과 비슷한듯)

    from returns.maybe import Maybe, Some
    
    Maybe.from_value(1)
    Maybe.from_optional(2)
    Maybe.from_optional(None)
    
    x = Some(2)
    y = Some(3)
    z: Maybe = Maybe.empty
    
    # map
    x.map(lambda a: a + 3)  # Some(5)
    
    # flatMap
    x.bind(lambda a: Some(a + 3))  # Some(5)
    
    # for-comprehension
    Maybe.do(
        a + b
        for a in x
        for b in y
    )  # Some(5)
    
    # pattern match
    match z:
        case Some(a):
            print(a)
        case Maybe.empty:
            print('nothing')

     

    'python > returns' 카테고리의 다른 글

    IO & IOResult  (0) 2022.09.04
    Result & ResultE  (0) 2022.09.04

    댓글

Designed by Tistory.