module fctmr.simplefctmr
#
Short summary#
module sparkouille.fctmr.simplefctmr
Simple mapper and reducer implemented in Python
Functions#
function |
truncated documentation |
---|---|
Joins (or combines) two generators. The function is written based on two reducers. The function is more efficient … |
|
Filters out elements from a generator. |
|
Applies function fct to a generator. |
|
Implements a reducer. |
|
Skips and takes elements from a generator. |
Documentation#
Simple mapper and reducer implemented in Python
- sparkouille.fctmr.simplefctmr.combiner(fctkey1, gen1, fctkey2, gen2, how='inner')#
Joins (or combines) two generators. The function is written based on two reducers. The function is more efficient if the groups of the second ensemble gen2 are shorter as each of them will be held in memory.
- Paramètres:
fctkey1 – function which returns the key for gen1
gen1 – generator for the first element
fctkey2 – function which returns the key for gen2
gen2 – generator for the second element
how – inner, outer, left, right*
- Renvoie:
generator
combiner or join
<<<
from sparkouille.fctmr import combiner def c0(el): return el[0] ens1 = [('a', 1), ('b', 2), ('a', 3)] ens2 = [('a', 10), ('b', 20), ('a', 30)] res = combiner(c0, ens1, c0, ens2) print(list(res))
>>>
[(('a', 1), ('a', 10)), (('a', 1), ('a', 30)), (('a', 3), ('a', 10)), (('a', 3), ('a', 30)), (('b', 2), ('b', 20))]
- sparkouille.fctmr.simplefctmr.ffilter(fct, gen)#
Filters out elements from a generator.
- Paramètres:
fct – function
gen – generator
- Renvoie:
generator
filter
<<<
from sparkouille.fctmr import ffilter res = ffilter(lambda x: x % 2 == 0, [4, 5]) print(list(res))
>>>
[4]
- sparkouille.fctmr.simplefctmr.mapper(fct, gen)#
Applies function fct to a generator.
- Paramètres:
fct – function
gen – generator
- Renvoie:
generator
mapper
<<<
from sparkouille.fctmr import mapper res = mapper(lambda x: x + 1, [4, 5]) print(list(res))
>>>
[5, 6]
Différence entre un itérateur et un générateur ?
Un itérateur et un générateur produisent tous deux des éléments issus d’un ensemble. La différence vient du fait que qu’un itérateur parcourt les éléments d’un ensemble qui existe en mémoire. Un générateur produit ou calcule des éléments d’un ensemble qui n’existe pas en mémoire. Par conséquent, parcourir deux fois un ensemble avec un itérateur a un coût en alors que pour un générateur, il faut ajouter le calcul de l’élément une seconde fois. Le coût est imprévisible et parfois il est préférable de cacher les éléments pour le parcourir plusieurs fois : cela revient à transformer un générateur en itérateur. Un générateur est souvent défini comme suit en Python :
<<<
def generate(some_iterator): for el in some_iterator: yield el g = generate([4, 5]) print(list(g)) print(g.__class__.__name__)
>>>
[4, 5] generator
- sparkouille.fctmr.simplefctmr.reducer(fctkey, gen, asiter=True, sort=True)#
Implements a reducer.
- Paramètres:
fctkey – function which returns the key
gen – generator
asiter – returns an iterator on each element of the group of the group itself
sort – sort elements by key before grouping
- Renvoie:
generator
reducer
<<<
from sparkouille.fctmr import reducer res = reducer(lambda x: x[0], [ ('a', 1), ('b', 2), ('a', 3)], asiter=False) print(list(res))
>>>
[('a', [('a', 1), ('a', 3)]), ('b', [('b', 2)])]
- sparkouille.fctmr.simplefctmr.take(gen, count=5, skip=0)#
Skips and takes elements from a generator.
- Paramètres:
gen – generator
count – number of elements to consider
skip – skip the first elements
- Renvoie:
generator
take
<<<
from sparkouille.fctmr import take res = take([4, 5, 6, 7, 8, 9], 2, 2) print(list(res))
>>>
[6, 7]