<s> __author__ = """<STR_LIT>""" <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> from os import getcwd <EOL> from os import sys <EOL> sys . path . append ( getcwd ( ) ) <EOL> from MOAL . helpers . display import Section <EOL> from MOAL . helpers . display import print_h2 <EOL> from MOAL . helpers . datamaker import random_person <EOL> import pymonad as pm <EOL> from pprint import pprint as ppr <EOL> DEBUG = True if __name__ == '<STR_LIT:__main__>' else False <EOL> @ pm . curry <EOL> def add2 ( x , y ) : <EOL> return x + y <EOL> @ pm . curry <EOL> def add_ten_to ( x , y ) : <EOL> return x + y + <NUM_LIT:10> <EOL> @ pm . curry <EOL> def add3 ( x , y , z ) : <EOL> return x + y + z <EOL> @ pm . curry <EOL> def add ( x , y ) : <EOL> return x + y <EOL> @ pm . curry <EOL> def sub2 ( x ) : <EOL> return x - <NUM_LIT:2> <EOL> @ pm . curry <EOL> def neg ( x ) : <EOL> return - x <EOL> def foofix ( word ) : <EOL> return '<STR_LIT>' + word <EOL> def negafy ( * vals ) : <EOL> """<STR_LIT>""" <EOL> return neg * pm . List ( * vals ) <EOL> def num_and_ranges ( x ) : <EOL> """<STR_LIT>""" <EOL> return pm . List ( * ( x , range ( - x , x + <NUM_LIT:1> ) ) ) <EOL> def monadify ( * funcs ) : <EOL> _monad = pm . List ( ) <EOL> for func in funcs : <EOL> _monad >> func <EOL> return _monad <EOL> def sqr ( x ) : <EOL> return pm . List ( x , x ** <NUM_LIT:2> ) <EOL> @ pm . curry <EOL> def exp ( x , pow ) : <EOL> return pm . List ( x , x ** pow ) <EOL> def monoid_range ( max ) : <EOL> """<STR_LIT>""" <EOL> return pm . List ( * range ( max ) ) <EOL> @ pm . curry <EOL> def s_add ( x , y ) : <EOL> return pm . State ( lambda old : ( x + y , old + <NUM_LIT:1> ) ) <EOL> @ pm . curry <EOL> def s_sub ( x , y ) : <EOL> return pm . State ( lambda old : ( x + y , old + <NUM_LIT:1> ) ) <EOL> @ pm . curry <EOL> def s_exp ( x , y ) : <EOL> return pm . State ( lambda old : ( x ** y , old + <NUM_LIT:1> ) ) <EOL> class Functor ( pm . Functor ) : <EOL> def fmap ( self , * args ) : <EOL> pass <EOL> class Applicative ( pm . Applicative ) : <EOL> def fmap ( self , * args ) : <EOL> pass <EOL> def amap ( self , * args ) : <EOL> pass <EOL> class Monad ( pm . Monad ) : <EOL> def bind ( self , * args ) : <EOL> pass <EOL> def fmap ( self , * args ) : <EOL> pass <EOL> def amap ( self , * args ) : <EOL> pass <EOL> if DEBUG : <EOL> with Section ( '<STR_LIT>' ) : <EOL> print_h2 ( '<STR_LIT>' ) <EOL> assert add2 ( <NUM_LIT:2> , <NUM_LIT:3> ) == add2 ( <NUM_LIT:2> ) ( <NUM_LIT:3> ) <EOL> assert add3 ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) == add3 ( <NUM_LIT:2> , <NUM_LIT:3> ) ( <NUM_LIT:4> ) <EOL> add_to_5 = add3 ( <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> print ( add_to_5 ( <NUM_LIT:10> ) ) <EOL> assert add3 ( <NUM_LIT:1> ) ( <NUM_LIT:2> ) ( <NUM_LIT:3> ) == add3 ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> print_h2 ( '<STR_LIT>' ) <EOL> composed = add * sub2 <EOL> assert composed ( <NUM_LIT:1> , <NUM_LIT:1> ) == <NUM_LIT:0> <EOL> comp_partial = add2 ( <NUM_LIT:2> ) * add3 ( <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> print ( comp_partial ( <NUM_LIT:2> ) ) <EOL> print_h2 ( '<STR_LIT>' ) <EOL> print ( neg * pm . List ( * range ( <NUM_LIT:10> ) ) ) <EOL> print ( neg * pm . Just ( <NUM_LIT:0> ) ) <EOL> print ( neg * pm . Nothing ) <EOL> nums = sub2 * pm . List ( * range ( <NUM_LIT:10> ) ) <EOL> print ( nums ) <EOL> nums2 = comp_partial * pm . List ( * range ( <NUM_LIT:4> ) ) <EOL> print ( nums2 ) <EOL> ppr ( [ sub2 * pm . List ( <EOL> * range ( n ) ) for n in comp_partial * pm . List ( * nums2 ) ] ) <EOL> print_h2 ( '<STR_LIT>' ) <EOL> bound = add2 * pm . List ( * range ( <NUM_LIT:3> ) ) & pm . List ( * range ( <NUM_LIT:3> ) ) <EOL> ppr ( [ bound , neg * bound ] ) <EOL> f = map ( lambda x : pm . List ( range ( x ) ) , range ( <NUM_LIT:10> ) ) <EOL> ppr ( f ) <EOL> f = add * monoid_range ( <NUM_LIT:4> ) & monoid_range ( <NUM_LIT:2> ) <EOL> print ( f ) <EOL> print ( neg * f ) <EOL> print_h2 ( '<STR_LIT>' ) <EOL> bound1 = add2 * pm . List ( * range ( <NUM_LIT:2> ) ) & pm . List ( * range ( <NUM_LIT:2> ) ) <EOL> bound2 = add_ten_to * pm . List ( * range ( <NUM_LIT:2> ) ) & bound1 <EOL> ppr ( bound2 ) <EOL> ppr ( num_and_ranges ( <NUM_LIT:10> ) ) <EOL> assert negafy ( * range ( <NUM_LIT:10> ) ) == neg * monoid_range ( <NUM_LIT:10> ) <EOL> names = pm . List ( * [ random_person ( ) [ '<STR_LIT:name>' ] for _ in range ( <NUM_LIT:4> ) ] ) <EOL> f1 = foofix * names <EOL> f2 = foofix * f1 <EOL> f3 = foofix * f2 <EOL> f4 = foofix * f3 <EOL> print ( f1 ) <EOL> print ( f2 ) <EOL> print ( f3 ) <EOL> print ( f4 ) <EOL> assert f4 [ <NUM_LIT:0> ] . startswith ( '<STR_LIT>' ) <EOL> print_h2 ( '<STR_LIT>' ) <EOL> f = monoid_range ( <NUM_LIT:4> ) >> monoid_range ( <NUM_LIT:4> ) <EOL> ppr ( neg * f ) <EOL> print ( pm . Just ( <NUM_LIT:9> ) >> pm . Just ( <NUM_LIT:10> ) ) <EOL> print ( monoid_range ( <NUM_LIT:3> ) ) <EOL> f = monoid_range ( <NUM_LIT:5> ) >> sqr >> exp ( <NUM_LIT:2> ) <EOL> ppr ( f ) <EOL> print_h2 ( '<STR_LIT>' ) <EOL> print_h2 ( '<STR_LIT>' ) <EOL> state1 = pm . unit ( pm . State , <NUM_LIT:1> ) >> s_add ( <NUM_LIT:3> ) >> s_exp ( <NUM_LIT:10> ) >> s_sub ( <NUM_LIT:3> ) <EOL> state2 = pm . unit ( pm . State , <NUM_LIT:1> ) >> s_add ( <NUM_LIT:10> ) >> s_exp ( <NUM_LIT:2> ) >> s_sub ( <NUM_LIT:100> ) <EOL> print ( state1 ( <NUM_LIT:4> ) ) <EOL> print ( state2 ( <NUM_LIT:4> ) ) </s>
