Refactoring

This commit is contained in:
Matthias Bisping 2023-02-07 11:44:01 +01:00
parent 092069221a
commit cb1c461049

View File

@ -147,27 +147,35 @@ def make_eithered_image_metadata_pair(image: Either, metadata: Either) -> Either
def context(value):
return {"reason": value, "metadata": metadata.either(bottom, identity)}
# What is most readable?
# Explicitly we are doing the following. (1) and (2) are equivalent.
# 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)
# a := Image
# b := Metadata
# c := ImageMetadataPair
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),
)
)
# 1)
# return (
# Right(make_image_metadata_pair) # m (a -> b -> c)
# .amap(image) # m (a -> b -> c) $ m a = m (b -> c)
# .amap(metadata) # m (b -> c) $ m b = m c
# .either(
# left(context),
# right(identity),
# )
# )
# 2)
# 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),
# )
# )
# Syntactic sugar variant:
return Either.apply(make_image_metadata_pair).to_arguments(image, metadata).either(left(context), right(identity))
@curry(2)