Last active
March 10, 2024 14:30
-
-
Save TGITS/751f7302723bfc29a269176d0c7aee22 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
import functools | |
def my_filter(predicate, seq): | |
def filter_and_aggregate_as_list(accumulator, current_elt): | |
if predicate(current_elt): | |
accumulator.append(current_elt) | |
return accumulator | |
return functools.reduce(filter_and_aggregate_as_list, seq, []) | |
print( | |
"Nombres pairs entre 1 à 9 avec my_filter:", | |
my_filter(lambda x: x % 2 == 0, range(1, 10)), | |
) | |
print( | |
"Nombres pairs entre 1 à 9 avec filter:", | |
list(filter(lambda x: x % 2 == 0, range(1, 10))), | |
) |
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
import math | |
import decimal | |
print( | |
"Somme de nombres flottants avec sum : ", | |
decimal.Decimal.from_float( | |
sum( | |
[ | |
1.234556e-20, | |
1.2e-10, | |
math.pi, | |
math.e, | |
math.sqrt(2), | |
math.tau, | |
math.sqrt(3), | |
] | |
) | |
), | |
) | |
print( | |
"Somme de nombres flottants avec fsum : ", | |
decimal.Decimal.from_float( | |
math.fsum( | |
[ | |
1.234556e-20, | |
1.2e-10, | |
math.pi, | |
math.e, | |
math.sqrt(2), | |
math.tau, | |
math.sqrt(3), | |
] | |
) | |
), | |
) |
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
import functools | |
def my_map(fun, seq): | |
def apply_function_and_aggregate_as_list(accumulator, current_elt): | |
accumulator.append(fun(current_elt)) | |
return accumulator | |
return functools.reduce(apply_function_and_aggregate_as_list, seq, []) | |
print( | |
"Puissances de 2 des nombres entre 1 à 9 avec my_map:", | |
my_map(lambda x: x**2, range(1, 10)), | |
) | |
print( | |
"Puissances de 2 des nombres entre 1 à 9 avec map:", | |
list(map(lambda x: x**2, range(1, 10))), | |
) |
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
import functools | |
import operator | |
# Exemple classique d'utilisation de reduce pour réaliser la somme d'une liste de nombres | |
# Solution classique avec une boucle for | |
sum_of_numbers_1 = 0 | |
for number in range(1, 11): | |
sum_of_numbers_1 = sum_of_numbers_1 + number | |
print("Somme des nombres de 1 à 10 avec une boucle for :", sum_of_numbers_1) | |
sum_of_numbers_2 = functools.reduce(lambda a, b: a + b, range(1, 11)) | |
print("Somme des nombres de 1 à 10 avec reduce :", sum_of_numbers_2) | |
sum_of_numbers_3 = functools.reduce(lambda a, b: a + b, [1]) | |
print("Somme de la liste à un élément [1] avec reduce :", sum_of_numbers_3) | |
# essayer de réduire une liste vide sans valeur initiale, par exemple functools.reduce(lambda a, b: a + b, []), | |
# provoque la levée d'une exception | |
sum_of_numbers_4 = functools.reduce(lambda a, b: a + b, [], 0) | |
print( | |
"Somme de la liste vide [] avec reduce et une valeur initiale :", sum_of_numbers_4 | |
) | |
sum_of_numbers_5 = functools.reduce(lambda a, b: a + b, range(1, 11), 0) | |
print( | |
"Somme des nombres de 1 à 10 avec reduce et une valeur initiale de 0 :", | |
sum_of_numbers_5, | |
) | |
sum_of_numbers_6 = functools.reduce(lambda a, b: a + b, range(1, 11), 5) | |
print( | |
"Somme des nombres de 1 à 10 avec reduce et une valeur initiale de 5 :", | |
sum_of_numbers_6, | |
) | |
# On peut utiliser l'opérateur add plutôt qu'une lambda ici | |
sum_of_numbers_7 = functools.reduce(operator.add, range(1, 11), 5) | |
print( | |
"Somme des nombres de 1 à 10 avec reduce et une valeur initiale de 5 :", | |
sum_of_numbers_7, | |
) |
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
import functools | |
import operator | |
import math | |
import decimal | |
# Exemple classique d'utilisation de reduce pour réaliser la somme d'une liste de nombres | |
numbers_1_to_10 = range(1, 11) | |
sum_numbers_1_to_10 = functools.reduce(operator.add, numbers_1_to_10) | |
print("Somme des nombres de 1 à 10 avec reduce :", sum_numbers_1_to_10) | |
print() | |
# Néanmoins Python propose directement la fonction native sum() pour ce cas d'utilisation | |
print("Somme des nombres de 1 à 10 avec sum :", sum(numbers_1_to_10)) | |
print() | |
print("Pareil mais avec une valeur de départ de 5 ajoutée à la somme") | |
# Utilisation de operator.add plutôt que de la lambda `lambda a, b: a + b` | |
sum_numbers_1_to_10_plus_5 = functools.reduce(operator.add, numbers_1_to_10, 5) | |
print( | |
"Somme des nombres de 1 à 10 avec une valeur initiale de 5 avec reduce :", | |
sum_numbers_1_to_10_plus_5, | |
) | |
print( | |
"Somme des nombres de 1 à 10 avec une valeur initiale de 5 avec sum :", | |
sum(numbers_1_to_10, 5), | |
) | |
# Pour les nombres flottants mieux vaut utiliser math.fsum que sum | |
print( | |
"Somme de nombres flottants avec sum : ", | |
decimal.Decimal.from_float( | |
sum([1.234556e-20, 1.2e-10, math.pi, math.e, math.sqrt(2)]) | |
), | |
) | |
print( | |
"Somme de nombres flottants avec fsum : ", | |
decimal.Decimal.from_float( | |
math.fsum([1.234556e-20, 1.2e-10, math.pi, math.e, math.sqrt(2)]) | |
), | |
) | |
print("\n##############\n") | |
# Autre exemple classique d'utilisation de reduce pour retourner le maximum ou le minimum | |
print( | |
"Le plus grand nombre pour l'intervalle de 1 à 10 avec reduce :", | |
functools.reduce(lambda a, b: a if a > b else b, numbers_1_to_10), | |
) | |
print( | |
"Le plus petit nombre pour l'intervalle de 1 à 10 avec reduce :", | |
functools.reduce(lambda a, b: a if a < b else b, numbers_1_to_10), | |
) | |
print() | |
# Néanmoins Python propose directement les fonctions native max() et min() pour ce cas d'utilisation | |
print( | |
"Le plus grand nombre pour l'intervalle de 1 à 10 avec max :", max(numbers_1_to_10) | |
) | |
print( | |
"Le plus petit nombre pour l'intervalle de 1 à 10 avec min :", min(numbers_1_to_10) | |
) | |
print("\n##############\n") | |
print( | |
"On n'est pas obligé de réduire à une valeur scalaire ! On peur réduire à n'importe quelle valeur !" | |
) | |
# Ensemble de maisons de Westeros | |
houses = {"tyrell", "stark", "lannister", "tarly", "baratheon", "targaryen"} | |
print("Houses :", houses) | |
def aggregate(accumulator, current_item): | |
accumulator.update({current_item: len(current_item)}) | |
return accumulator | |
length_by_house = functools.reduce(aggregate, houses, {}) | |
print("Longueur du nom par nom de maison avec reduce:", length_by_house) | |
print( | |
"Longueur du nom par nom de maison avec compréhension:", | |
{house: len(house) for house in houses}, | |
) | |
class HousesOfWesteros: | |
def __init__(self): | |
self.name_length_by_name = {} | |
def update(self, dictionary): | |
self.name_length_by_name.update(dictionary) | |
def __str__(self): | |
return str(self.name_length_by_name) | |
print( | |
"Longueur du nom par nom de maison avec reduce sur un objet :", | |
functools.reduce(aggregate, houses, HousesOfWesteros()), | |
) |
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
import functools | |
houses = {"tyrell", "stark", "lannister", "tarly", "baratheon", "targaryen"} | |
print("Houses :", houses) | |
def aggregate(accumulator, current_item): | |
accumulator.update({current_item: len(current_item)}) | |
return accumulator | |
length_by_house = functools.reduce(aggregate, houses, {}) | |
print("Longueur du nom par nom de maison avec reduce:", length_by_house) |
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
import functools | |
houses = {"tyrell", "stark", "lannister", "tarly", "baratheon", "targaryen"} | |
def aggregate(accumulator, current_item): | |
accumulator.update({current_item: len(current_item)}) | |
return accumulator | |
class HousesOfWesteros: | |
def __init__(self): | |
self.name_length_by_name = {} | |
def update(self, dictionary): | |
self.name_length_by_name.update(dictionary) | |
def __str__(self): | |
return str(self.name_length_by_name) | |
print( | |
"Longueur du nom par nom de maison avec reduce sur un objet :", | |
functools.reduce(aggregate, houses, HousesOfWesteros()), | |
) |
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
houses = {"tyrell", "stark", "lannister", "tarly", "baratheon", "targaryen"} | |
print("Houses :", houses) | |
print( | |
"Longueur du nom par nom de maison avec compréhension:", | |
{house: len(house) for house in houses}, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment