#Find Squares of a List
a = [1, 2, 3, 4]
def f(n):
'''Return square of n.'''
return pow(n, 2)
x = []
for v in a:
x.append(f(v))
y = [f(b) for b in a]
z = map(f, a)
print x #[1, 4, 9, 16]
print y #[1, 4, 9, 16]
print z #[1, 4, 9, 16]
f(n)
is as functional as it gets.x
, y
, & z
, all three get the job done.
def mess(x):
'''Bad version of pow(x, 2).'''
x = int(x)
print 'mess running: %d' % x
x = x * x
print 'mess returning: %d' % x
if x:
return x
else:
return None
print map(mess, a)
#[1, 4, 9, 16]
mess
is NOT functional.
g = mess
print map(g, a)
#[1, 4, 9, 16]
for_loop = map
print for_loop(g, a)
#[1, 4, 9, 16]
f(x)
or g(x)
& map(f, x)
or for_loop(g, a)
is purely aesthetic.
#Functional #Non-Functional
x = 1 x = 1
y = x + 1 x = x + 1
z = map(f, x) x = map(f, x)
--Sample Haskell 'Hello World' Code
main :: IO ()
main = do
putStrLn "Hello World"
do
is a procedural escape (i.e. it's not Functional).run_tot
reduces to (i.e. ~ points towards ) the same thing all the time.
x = my_first_thought(data_1)
y = something_better()
z = what_should_have_come_first(data_2)
solution_set = f(y, z)
#The Output Requested
print solution_set
x
is never used.my_first_thought()
never used.y
has no inputs, so its a static data set.range(10, 110, 10)
f(y, z)
has no side effects (by definition).what_should_have_come_first(data_2)
.x
, y
, & z
share no inputs
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
Good Bad
#Return a single value type:
def adder(x, y): def adder(x, y):
return x + y if x and y:
return x + y
else:
return 'Need two ints'
#Isolate side effects
def updater(p_num): def update_pr(p_num):
p_num.p2 = pow(num, 2) p_num.p2 = pow(num, 2)
p_num.p3 = pow(num, 3) p_num.p3 = pow(num, 3)
return (p_num.p2, p_num.p3)
t = (p_num.p2, p_num.p3)
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]
#Simple Map (list to list)
x = range(10)
map_1 = map(lambda x: x*x, x)
map_2 = [a*a for a in x]
#x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
#map_1 = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
#map_2 = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
#Simple Reduce (list to scalar)
reduce_1 = sum(x)
#x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
#reduce_1 = 45
reduce_2 = 0
while x:
reduce_2 += x.pop()
#x = []
#reduce_2 = 45
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()
#pre-process
# Main Process
#post-process
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)
#Fahrenheit in: 25.0
#Water is solid at -3.89C
# function call
#Water is liquid at 6.11C
#Fahrenheit out: 43.0
#Returned Value: 43.00
print 'Returned Value: %.2f' % warmer_Fahrenheit(Fahrenheit=25)
#Fahrenheit in: 25.0
#Water is solid at -3.89C
# function call
#Water is liquid at 6.11C
#Fahrenheit out: 43.0
#Returned Value: 43.00
@functional_programming
def python_code():
do_stuff_functionally_for_a_change()
@procedural_programming
def haskell_code():
do_stuff_procedurally_for_a_change()
#Pre-filling with a function
curry_func = lambda y: map(lambda x: x*x, y)
print curry_func(range(5))
#[0, 1, 4, 9, 16]
#Pre-filling with data
curry_data = lambda y: map(y, range(5))
print curry_data(lambda x : x*x)
#[0, 1, 4, 9, 16]