#!/usr/bin/env python
class MonadLisa:
"""A Monad-ish structure containing the Mona Lisa."""
def __init__(self, func):
"""Set Mona Lisa inner state, execute function."""
self._mona = """
____
o8%8888,
o88%8888888.
8'- -:8888b
8' 8888
d8.-=. ,==-.:888b
>8 `~` :`~' d8888
88 ,88888
88b. `-~ ':88888
888b --' .:88888
88888o--:':::8888
`88888| :::' 8888b
8888^^' 8888b
d888 ,%888b.
d88% %%%8--'-.
/88:.__ , _%-' --- -
'''::===..-' = --. `
"""
self.error = None
self.result = None
self._run(func)
def _run(self, func):
"""Run function on inner state, set result or error."""
try:
self.result = func(self._mona)
except Exception as e:
self.error = e
def run_examples():
"""Examples of the MonadLisa class."""
print("Monad Lisa examples")
print("Each run instantiates a MonadLisa")
print("and gives it a different function")
print("\nm1 is the built-in 'print', causing below side effect")
m1 = MonadLisa(print)
print("m1 error:", m1.error)
print("m1 result:", m1.result)
print("\nm2 is the identity function")
m2 = MonadLisa(lambda x: x)
print("m2 error:", m2.error)
print("m2 result:", m2.result)
print("\nm3 is the built-in 'abs'")
m3 = MonadLisa(abs)
print("m3 error:", m3.error)
print("m3 result:", m3.result)
run_examples()
Comments