aboutsummaryrefslogtreecommitdiff
path: root/functional_tools/__init__.py
blob: 057bd9ac0622a69b2fc27eb454012c09b62e2d7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""Tools to help with a more functional way of doing things."""
from typing import Iterable
from functools import reduce

def take(iterable: Iterable, num: int) -> list:
    """Take at most `num` items from `iterable`."""
    iterator = iter(iterable)
    items = []
    try:
        for i in range(0, num): # pylint: disable=[unused-variable]
            items.append(next(iterator))

        return items
    except StopIteration:
        return items

def chain(value, *functions):
    """
    Flatten nested expressions

    Inspired by, and approximates, Clojure's `->`.

    Useful to rewrite nested expressions like func3(a, b, func2(c, func1(d e)))
    into arguably flatter expressions like:
    chain(
      d,
      partial(func1, e=val1),
      partial(func2, c=val2),
      partial(func3, a=val3, b=val3))

    This can probably be improved.
    """
    return reduce(lambda result, func: func(result), functions, value)