How to curry functions in python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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)

11th Oct 2020, 2:40 PM
Gsturg
Gsturg - avatar
7 Answers
+ 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
11th Oct 2020, 10:28 PM
Kevin ★
+ 1
Currying means? how?
11th Oct 2020, 2:55 PM
Jayakrishna 🇮🇳
11th Oct 2020, 3:04 PM
Sousou
Sousou - avatar
0
like as in if f(x) = x+3 g(x) = x+5 then f(g(x)) = x + 3 + 5
11th Oct 2020, 3:01 PM
Gsturg
Gsturg - avatar
0
yes Sousou thank you thats what i was looking for
11th Oct 2020, 3:14 PM
Gsturg
Gsturg - avatar
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..
11th Oct 2020, 3:15 PM
Jayakrishna 🇮🇳
0
Exactly the same solution I thought, as by Sousou https://code.sololearn.com/cSJQ9XbD2l39/?ref=app
11th Oct 2020, 3:25 PM
Abhay
Abhay - avatar