File: pipe.py

package info (click to toggle)
python-returns 0.26.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,652 kB
  • sloc: python: 11,000; makefile: 18
file content (34 lines) | stat: -rw-r--r-- 1,016 bytes parent folder | download | duplicates (2)
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
34
from returns._internal.pipeline.flow import flow


def pipe(*functions):
    """
    Allows to compose a value and up to 7 functions that use this value.

    We use a custom ``mypy`` plugin to make sure types are correct.
    Otherwise, it is currently impossible to properly type this function.

    Each next function uses the previous result as an input parameter.
    Here's how it should be used:

    .. code:: python

       >>> from returns.pipeline import pipe

       >>> # => executes: str(float(int('1')))
       >>> assert pipe(int, float, str)('1') == '1.0'

    This function is closely related
    to :func:`pipe <returns._internal.pipeline.flow.flow>`:

    .. code:: python

      >>> from returns.pipeline import flow
      >>> assert pipe(int, float, str)('1') == flow('1', int, float, str)

    See also:
        - https://stackoverflow.com/a/41585450/4842742
        - https://github.com/gcanti/fp-ts/blob/master/src/pipeable.ts

    """
    return lambda instance: flow(instance, *functions)