''' Created on Sep 1, 2015 @author: Brett Paufler Copyright Brett Paufler Worksheet - Nothing worth saving - Can Kill Rolling Worksheet for Functional Programming webpage --- Inspired, Lightning Talk-ish Likely can kill the lot But does have nice wrapper examples ''' def f(x): '''Return square of x.''' return pow(x, 2) ''' a = [1, 2, 3, 4] x = [] for v in a: x.append(f(v)) y = map(f, a) z = [f(b) for b in a] print x #[1, 4, 9, 16] print y #[1, 4, 9, 16] print z #[1, 4, 9, 16] ''' def mess(x): '''Given an int, float, or some numeric representation of same function mess returns the squared value of closest int to passed value. Hopefully this doc string seems a bit long winded and confusing, print the word mess, and it goes right to my head.''' x = int(x) x = x * x if x: return x ''' print map(mess, a) print 'at g' g = mess print map(g, a) for_loop = map print for_loop(g, a) print '\n\n\nNEW HERE:\n\n\n' items = [] run_tot = 0 for i in range(10): items.append(i) run_tot += i print i, run_tot, items print print i, run_tot, items items = range(10) run_tot = sum(items) print '\n\n\n\nReady Int\n\n\n\n' ''' class powers(int): def __new__(self, num): return int.__new__(self, num) def __init__(self, num): self.p1 = pow(num, 1) self.p2 = pow(num, 2) self.p3 = pow(num, 3) def __add__(self, r_int): return self + r_int def state(self): print 'Base: %d, Square: %d, Cube: %d' % (self.p1, self.p2, self.p3) ''' p = powers(3) p.state() #Base: 3, Square: 9, Cube: 27 p.p1 = 10 p.state() #Base: 10, Square: 9, Cube: 27 #from functools import wraps ''' ''' def round_trip(func, *args, **kwargs): def wrapper(func, *args, **kwargs): return func return wrapper def doubler(i): return i + i @round_trip doubler(3) ''' ''' #from itertools import compress def long_form_of_even(n): test = n % 2 == 0 if test: return True else: return False def even(n): return n % 2 == 0 to_ten = range(10) #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] even_to_ten = [t for t in to_ten if even(t)] #[0, 2, 4, 6, 8] even_to_ten = filter(even, to_ten) #[0, 2, 4, 6, 8] from itertools import compress mask = [even(t) for t in to_ten] #[True, False, True, False, True, False, True, False, True, False] even_to_ten = list(compress(to_ten, mask)) #[0, 2, 4, 6, 8] print even_to_ten ''' ''' print '\n\n\n\nLIST:\n' x = range(10) map_1 = map(lambda x: x*x, x) map_2 = [a*a for a in x] print x print map_1 print map_2 print reduce_1 = sum(x) print x print reduce_1 reduce_2 = 0 while x: reduce_2 += x.pop() print x print reduce_2 ''' def pre_post_pass_through(func): def whatever(): print 'pre-process' func() print 'post-process' return whatever @pre_post_pass_through def base_function(): print '\tMain Process' base_function() print '\n\n\n\n' def water_state(Celsius): '''Returns a string indicating water phase at provided temperature in Celsius.''' if Celsius <= 0: t = 'solid' elif Celsius < 100: t = 'liquid' else: t = 'gaseous' return t def warmer(Celsius): '''Adds 10 degrees to Celsius, making it warmer.''' return 10.0 + Celsius def report(Celsius): '''Returns text string reporting phase/temp.''' return 'Water is {} at {:.2f}C'.format( water_state(Celsius), Celsius) def Celsius_wrapper(func): '''Round trip conversion from Fahrenheit to Celsius and back, executing wrapped function using units in Celsius.''' def wrapper(Fahrenheit): #conversion in (pre-process) Celsius = (Fahrenheit - 32.0) * 5.0/9.0 print 'Fahrenheit in: ', round(Fahrenheit, 2) print report(Celsius) #Execution of wrapped function Celsius = func(Celsius) print '\tfunction call' #conversion out (post-process) Fahrenheit = Celsius * 9.0 /5.0 + 32.0 print report(Celsius) print 'Fahrenheit out: ', round(Fahrenheit, 2) return Fahrenheit return wrapper @Celsius_wrapper def warmer_Fahrenheit(Fahrenheit): return warmer(Fahrenheit) print 'Returned Value: %.2f' % warmer_Fahrenheit(Fahrenheit=25) print 'Returned Value: %.2f' % warmer_Fahrenheit(Fahrenheit=25) print '\n\n\n ' data = 0 #from math import sin curry_func = lambda y: map(lambda x: x*x, y) print curry_func(range(5)) curry_data = lambda y: map(y, range(5)) print curry_data(lambda x : x*x)