Created
November 29, 2017 21:14
-
-
Save johnhonan/21591086aaaec312d787dfb6f42a4ca0 to your computer and use it in GitHub Desktop.
calculator which can take lists and uses map-reduce functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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