0
How to curry functions in python?
I want to make this function simpler using currying. lst = [i for i in range(100)] def divisible_by_6(lst): new_list = [] for i in lst: if i % 2 == 0: new_list.append(i) final_list = [] for j in new_list: if j % 3 == 0: final_list.append(j) return final_list divisible_by_6(lst)
7 Respostas
+ 3
Gyasi Sturgis
The answers given and your example are not currying but function composition.
I don't think that currying will make your original function simpler, if you want to make it simpler you can just do:
def divisible_by_6(lst):
return [*filter(lambda x:x%6==0,lst)]
Currying is about a function that splits its functionality into one or many unary(single parameter) functions. If you want an example anyway:
https://code.sololearn.com/c1956CiHV3Iv/?ref=app
+ 1
Currying means? how?
+ 1
Do you mean like this?
https://code.sololearn.com/cto6W0V7X8ta/?ref=app
0
like as in if
f(x) = x+3
g(x) = x+5 then
f(g(x)) = x + 3 + 5
0
yes Sousou thank you thats what i was looking for
0
lst = [i for i in range(100)]
def divisible_by_2(lst):
new_list = []
for i in lst:
if i % 2 == 0:
new_list.append(i)
return new_list
def divisible_by_3(lst):
final_list = []
for j in lst:
if j % 3 == 0:
final_list.append(j)
return final_list
def divisible_by_6(lst):
new_list = divisible_by_2(lst)
final_list = divisible_by_3(new_list)
return final_list
print( divisible_by_6(lst))
#like this? But it makes redundant code..
0
Exactly the same solution I thought, as by Sousou
https://code.sololearn.com/cSJQ9XbD2l39/?ref=app