Skip to content

Instantly share code, notes, and snippets.

@johnhonan
Created November 29, 2017 21:14
Show Gist options
  • Save johnhonan/21591086aaaec312d787dfb6f42a4ca0 to your computer and use it in GitHub Desktop.
Save johnhonan/21591086aaaec312d787dfb6f42a4ca0 to your computer and use it in GitHub Desktop.
calculator which can take lists and uses map-reduce functions
class Calculator(object): # define a calculator object
def add(self, x): # define addition function
# use comprehebnsion to check that all
# elements in a list are numeric
number_types = (int, long, float, complex) # accepted number types
if all(isinstance(item, number_types) for item in x):
return reduce(lambda a,b: a+b, x)
else:
raise ValueError # if one or both values are not accepted number types return a Value Error
def subtract(self, x): # define addition function
return reduce(lambda a,b: a-b, x)
def mult(self, x): # define addition function
return reduce(lambda a,b: a*b, x)
def square(self, x):
return map(lambda a: a*a, x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment