random par ou impar (functional example)
"""
Exemplo para ajudar a responder o problema de aninhamento em https://gist.github.com/AndersonFirmino/b0300923094a5a8450018c5bd32c9de8
"""
from itertools import filterfalse
from functools import partial
from random import choice, random
def pipe(*funcs):
def inner(data, funcs=funcs):
result = data
for f in funcs:
result = f(result)
return result
return inner
is_par = lambda x: x % 2 == 0
filter_par = partial(filter, is_par)
filter_impar = partial(filterfalse, is_par)
random_sorted = partial(sorted, key=lambda x: random())
flaged = pipe(range, tuple, filter_par, random_sorted, choice)
non_flaged = pipe(range, tuple, filter_impar, random_sorted, choice)
def odd_or_even(flag, limit):
"""Retorna um numero aleatorio pelo limite
:param flag:
:param limit:
:return:
"""
return flaged(limit) if flag else non_flaged(limit)