Created
April 21, 2021 13:59
-
-
Save openbrian/5c92ed9e3bdb981823673c81c616dd85 to your computer and use it in GitHub Desktop.
Dictionary incompatible types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Dict, TypeVar | |
from returns.curry import partial | |
from returns.result import Failure, Success, Result | |
T = TypeVar("T") | |
def get_dict_item(needle: str, haystack: Dict[str, T]) -> Result[T, str]: | |
if needle not in haystack: | |
return Failure(f"Dictionary did not have key {needle}.") | |
return Success(haystack[needle]) | |
reveal_type(get_dict_item) | |
get_a = partial(get_dict_item, 'a') | |
reveal_type(get_a) | |
#get_b = partial(get_dict_item, 'b') | |
#reveal_type(get_b) | |
Success({'a': {'b': 1}}).bind(get_a) | |
#Success({'a': {'b': 1}}).bind(get_a).bind(get_b) | |
# Would be nice to compose somehting like xpath thing.bind(get_path("a/b")) |
Author
openbrian
commented
Apr 21, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment