@@ -9,55 +9,89 @@ composition easy, readable, pythonic, and useful.
99Let's start with the first one.
1010
1111
12+ flow
13+ ----
14+
15+ ``flow `` allows to easily compose multiple functions together.
16+ It is useful when you already have an instance to compose functions with.
17+
18+ Let's see an example.
19+
20+ .. code :: python
21+
22+ >> > from returns.pipeline import flow
23+ >> > assert flow(
24+ ... [1 , 2 , 3 ],
25+ ... lambda collection : max (collection),
26+ ... lambda max_number : - max_number,
27+ ... ) == - 3
28+
29+ Use it when you need to compose a lot of functions together.
30+
31+ And now let's get to know ``pipe ``, it is very similar,
32+ but has different usage pattern.
33+
34+
1235.. _pipe :
1336
1437pipe
1538----
1639
1740``pipe `` is an easy way to compose functions together.
41+ It is useful when you don't have an instance to compose functions with yet.
42+
1843Let's see an example.
1944
2045.. code :: python
2146
2247 >> > from returns.pipeline import pipe
2348
24- >> > pipe(str , lambda x : x + ' b' , str .upper)( 1 )
25- ' 1B'
49+ >> > pipeline = pipe(str , lambda x : x + ' b' , str .upper)
50+ >> > assert pipeline( 1 ) == ' 1B'
2651
27- There's also a way to compose containers together :
52+ It might be later used with multiple values :
2853
2954.. code :: python
3055
31- from returns.pipeline import pipe
32- from returns.result import Result
33- from returns.functions import box
34-
35- def regular_function (arg : int ) -> float :
36- ...
56+ >> > assert pipeline(2 ) == ' 2B'
3757
38- def returns_container (arg : float ) -> Result[complex , ValueError ]:
39- ...
58+ It is also might be useful to compose containers together:
4059
41- def also_returns_container (args : complex ) -> Result[str , ValueError ]:
42- ...
60+ .. code :: python
4361
44- pipe(
45- regular_function, # composes easily
46- returns_container, # also composes easily, but returns a container...
47- # So we need to `box` the next function to allow it to consume
48- # the container from the previous step.
49- box(also_returns_container),
50- )(1 ) # we provide the initial value itself as a argument to `pipe(...)`
51- # => Will return `Result[str, ValueError]` as declared in the last step
62+ >> > from returns.pipeline import pipe
63+ >> > from returns.result import Result, Success, Failure
64+ >> > from returns.pointfree import bind
65+
66+ >> > def regular_function (arg : int ) -> float :
67+ ... return float (arg)
68+ ...
69+
70+ >> > def returns_container (arg : float ) -> Result[str , ValueError ]:
71+ ... if arg != 0 :
72+ ... return Success(str (arg))
73+ ... return Failure(ValueError ())
74+ ...
75+
76+ >> > def also_returns_container (arg : str ) -> Result[str , ValueError ]:
77+ ... return Success(arg + ' !' )
78+ ...
79+
80+ >> > transaction = pipe(
81+ ... regular_function, # composes easily
82+ ... returns_container, # also composes easily, but returns a container
83+ ... # So we need to `bind` the next function to allow it to consume
84+ ... # the container from the previous step.
85+ ... bind(also_returns_container),
86+ ... )
87+ >> > result = transaction(1 ) # running the pipeline
88+ >> > assert result == Success(' 1.0!' )
5289
5390 You might consider ``pipe() `` as :func: `returns.functions.compose ` on steroids.
5491The main difference is that ``compose `` takes strictly two arguments
5592(or you might say that it has an arity of two),
5693while ``pipe `` has infinite possible arguments.
5794
58- See also :func: `returns.io.IO.lift ` which is also extremely
59- helpful for ``IO `` composition.
60-
6195Limitations
6296~~~~~~~~~~~
6397
@@ -69,6 +103,8 @@ But, composition with ``pipe`` is limited to two things:
691032. It is flexible. Sometimes you might need more power.
70104 Use ``@pipeline `` in this case!
71105
106+ In contrast ``flow `` does not have these problems.
107+
72108
73109.. _pipeline :
74110
0 commit comments