All the programs below implement these steps:
- Generate a list of 1-8, not including 8: [1, 2, 3, 4, 5, 6, 7]
- Filter that list to even values: [2, 4, 6]
- Return each value N repeated N times: [2, 2, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6]
All the programs below implement these steps:
from pydash import py_ | |
def is_even(n): | |
return n % 2 == 0 | |
def repeat(n, times): | |
return [n] * times | |
def concat(list_a, list_b): | |
return [*list_a, *list_b] | |
def computed(): | |
return ( | |
py_(py_.range(1, 8)) | |
.filter(is_even) | |
.map(lambda n: repeat(n, n)) | |
.reduce(concat, []) | |
.value() | |
) | |
print(computed()) | |
# > python compute-with-python.py | |
# [2, 2, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6] |
from functools import reduce | |
def is_even(n): | |
return n % 2 == 0 | |
def repeat(n, times): | |
return [n] * times | |
def concat(list_a, list_b): | |
return [*list_a, *list_b] | |
def computed(): | |
numbers = range(1, 8) | |
evens = filter(is_even, numbers) | |
repeats = map(lambda n: repeat(n, n), evens) | |
values = reduce(concat, repeats, []) | |
return values | |
print(computed()) | |
# > python compute.py | |
# [2, 2, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6] |