From 0ce49095d62ae46f5de8a42f67d1a38de26c8763 Mon Sep 17 00:00:00 2001 From: Hizenberg469 Date: Mon, 10 Mar 2025 16:45:40 +0000 Subject: [PATCH] Functional programming finished --- Functional-programming/list_comp.py | 21 ++++++ Functional-programming/reduce.py | 19 +++++ Functional-programming/wiki.txt | 104 ++++++++++++++++++++++++++++ 3 files changed, 144 insertions(+) create mode 100644 Functional-programming/list_comp.py create mode 100644 Functional-programming/reduce.py diff --git a/Functional-programming/list_comp.py b/Functional-programming/list_comp.py new file mode 100644 index 0000000..f293325 --- /dev/null +++ b/Functional-programming/list_comp.py @@ -0,0 +1,21 @@ +my_list = [char for char in 'hello'] +my_list2 = [num*2 for num in range(0,100)] + +my_list3 = [num for num in range(0,100) if num % 2 == 0] + +simple_dict = { + 'a' : 1, + 'b' : 2 +} + +my_dict = {key:value for key,value in simple_dict.items()} + +my_dict1 = {num: num*2 for num in [1,2,3]} +# print(my_list) +# print(my_list3) + +print(my_dict) +print(my_dict1) + +# for in +# same for set comprehension \ No newline at end of file diff --git a/Functional-programming/reduce.py b/Functional-programming/reduce.py new file mode 100644 index 0000000..cacaec5 --- /dev/null +++ b/Functional-programming/reduce.py @@ -0,0 +1,19 @@ +from functools import reduce + +my_list = [1,2,3] + +def accumalator(acc, item): + print(acc, item) + return acc + item + +print(reduce(accumalator, my_list, 10)) + +# What reduce does? + +# reduce take the function given and take item from the +# iterable and pass to function given to it. The first +# parameter is the value initialized as the 3rd argument +# to the reduce function. It do the processing and returns +# a value which is used as next initialiizing value for the +# first argument of the function provided in the reduce function +# and so on for all items in iterable. \ No newline at end of file diff --git a/Functional-programming/wiki.txt b/Functional-programming/wiki.txt index e69de29..6f47dd6 100644 --- a/Functional-programming/wiki.txt +++ b/Functional-programming/wiki.txt @@ -0,0 +1,104 @@ +-> Common function used in python: + + map, filter, zip, and replace + +-> map: + + Prototype: + + map(, ) + + Whatever the first argument, i.e., function can do, map will do + the same logic for each item in the iterable and return the + iterable with same structure. + + + for ex: + + my_list = [1,2,3] + def multiply_by2(item): + return item*2 + + print(list(map(multiply_by2, my_list))) + print(my_list) + +-> filter: + + Prototype: + + filter(, ) + + To filter some items from the iterable based on the condition + defined in the function which return some boolean value. + + + for ex: + + my_list = [1,2,3] + def only_odd(item): + return item % 2 == 0 + + print(list(filter(only_odd, my_list))) + print(my_list) + + +-> zip: + + Prototype: + + zip( , ) + + combine each items from both iterable together. + + + for ex: + + my_list = [1, 2, 3] + your_list = [10, 20, 30] + + print(list(zip(my_list, your_list))) + print(my_list) + + Output: + + [(1,10), (2,20), (3,30)] + +-> reduce: + + to use reduce add the below line: + + from functools import reduce + + + # What reduce does? + + # reduce take the function given and take item from the + # iterable and pass to function given to it. The first + # parameter is the value initialized as the 3rd argument + # to the reduce function. It do the processing and returns + # a value which is used as next initialiizing value for the + # first argument of the function provided in the reduce function + # and so on for all items in iterable. + +-> lambda expressions: + + one time anonymous function. + + Prototype: + + lambda item: + + + for ex: + + my_list = [1,2,3] + + print(list(map(lambda item: item*2, my_list))) + +-> list comprehension: + + for ex: + + my_list = [char for char in 'hello'] + + print(my_list) \ No newline at end of file