# Function Composition Example
# Python 3 Reflexive Metaprogramming
# H. Conrad Cunningham, Professor, Computer and Information Science
# University of Mississippi

# Developed for CSci 658, Software Language Engineering, Spring 2018

#234567890123456789012345678901234567890123456789012345678901234567890

# 2018-03-18: (V1)

def compose2(f, g):
    def comp(x):
	    return f(g(x))
    return comp

def compose2b(f, g): 
	return lambda x: f(g(x))

def square(x):
    return x * x

def inc(x):
    return x + 1

if __name__ == '__main__':
    print('Using compose2')
    inc_then_square = compose2(square, inc)
    print(f'inc_then_square(0)  = {inc_then_square(0)}')
    print(f'inc_then_square(1)  = {inc_then_square(1)}')
    print(f'inc_then_square(10) = {inc_then_square(10)}')
    print(f'inc_then_square(11) = {inc_then_square(11)}')
    print('Using compose2b')
    inc_then_square = compose2b(square, inc)
    print(f'inc_then_square(0)  = {inc_then_square(0)}')
    print(f'inc_then_square(1)  = {inc_then_square(1)}')
    print(f'inc_then_square(10) = {inc_then_square(10)}')
    print(f'inc_then_square(11) = {inc_then_square(11)}')
