[WIP] Either refactoring
Add alternative formulation for monadic chain
This commit is contained in:
parent
066cf17add
commit
dffe1c18fc
@ -65,7 +65,7 @@ def take_right(pair: Either):
|
|||||||
|
|
||||||
|
|
||||||
def format_context(context):
|
def format_context(context):
|
||||||
return f"Reason: {context['reason']}. Metadata: {EnumFormatter()(context['metadata'])}"
|
return f"Reason: {context['reason'].rstrip('.')}. Metadata: {EnumFormatter()(context['metadata'])}"
|
||||||
|
|
||||||
|
|
||||||
def extract_pages(doc, page_range):
|
def extract_pages(doc, page_range):
|
||||||
@ -131,16 +131,29 @@ def xref_to_maybe_image(doc, xref) -> Either:
|
|||||||
|
|
||||||
|
|
||||||
def make_maybe_image_metadata_pair(image: Either, metadata: Either):
|
def make_maybe_image_metadata_pair(image: Either, metadata: Either):
|
||||||
"""
|
"""Reference: haskell.org/tutorial/monads.html"""
|
||||||
Reference:
|
|
||||||
haskell.org/tutorial/monads.html
|
|
||||||
(>>) :: m a -> m b -> m b
|
|
||||||
"""
|
|
||||||
|
|
||||||
def context(value):
|
def context(value):
|
||||||
return {"reason": value, "metadata": metadata.either(bottom, identity)}
|
return {"reason": value, "metadata": metadata.either(bottom, identity)}
|
||||||
|
|
||||||
return Right(make_image_metadata_pair).amap(image).amap(metadata).either(left(context), right(identity))
|
# What is most readable?
|
||||||
|
|
||||||
|
# 1)
|
||||||
|
# return Either.apply(make_image_metadata_pair).to_arguments(image, metadata).either(left(context), right(identity))
|
||||||
|
|
||||||
|
# 2)
|
||||||
|
# m (a -> b -> c) -> m a -> m b -> m c
|
||||||
|
# return Right(make_image_metadata_pair).amap(image).amap(metadata).either(left(context), right(identity))
|
||||||
|
|
||||||
|
# 3)
|
||||||
|
return (
|
||||||
|
image.bind(right(make_image_metadata_pair)) # m a >>= (m (a -> b -> c)) -> m (b -> c)
|
||||||
|
.amap(metadata) # m (b -> c) <*> m b -> m c
|
||||||
|
.either(
|
||||||
|
left(context),
|
||||||
|
right(identity),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@curry(2)
|
@curry(2)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user