Last active
November 29, 2015 21:01
-
-
Save GitMoIO/69ce814864ffb991d58f to your computer and use it in GitHub Desktop.
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
def factory(x): | |
def multiply(my_array): | |
my_array2 = [] | |
for each in my_array: | |
my_array2.append(each*x) | |
return my_array2 | |
return multiply | |
#using lambda | |
def factory(x): | |
return lambda arr: [x * el for el in arr] | |
#Another way of passing a function to another function is using a callable object as follows: | |
class factory: | |
def __init__(self, x): | |
self.x = x | |
def __call__(self, arr1): | |
arr2 = [] | |
for each in arr1: | |
arr2.append(each * self.x) | |
return arr2 | |
#Using lambda | |
#My Test Cases | |
#my_arr = [1, 2, 3] | |
#threes = factory(3) | |
#test.assert_equals(threes(my_arr), [3, 6, 9]) | |
#fives = factory(5) | |
#test.assert_equals(fives(my_arr), [5, 10, 15]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment