MCPcopy
hub / github.com/dry-python/returns / bind

Method bind

returns/context/requires_context.py:176–209  ·  view source on GitHub ↗

Composes a container with a function returning another container. This is useful when you do several computations that rely on the same context. .. code:: python >>> from returns.context import RequiresContext >>> def first(lg: bool) -> Requir

(
        self,
        function: Callable[
            [_ReturnType_co],
            Kind2[RequiresContext, _NewReturnType, _EnvType_contra],
        ],
    )

Source from the content-addressed store, hash-verified

174 )
175
176 def bind(
177 self,
178 function: Callable[
179 [_ReturnType_co],
180 Kind2[RequiresContext, _NewReturnType, _EnvType_contra],
181 ],
182 ) -> RequiresContext[_NewReturnType, _EnvType_contra]:
183 """
184 Composes a container with a function returning another container.
185
186 This is useful when you do several computations that rely on the
187 same context.
188
189 .. code:: python
190
191 >>> from returns.context import RequiresContext
192
193 >>> def first(lg: bool) -> RequiresContext[int, float]:
194 ... # `deps` has `float` type here:
195 ... return RequiresContext(
196 ... lambda deps: deps if lg else -deps,
197 ... )
198
199 >>> def second(number: int) -> RequiresContext[str, float]:
200 ... # `deps` has `float` type here:
201 ... return RequiresContext(
202 ... lambda deps: '>=' if number >= deps else '<',
203 ... )
204
205 >>> assert first(True).bind(second)(1) == '>='
206 >>> assert first(False).bind(second)(2) == '<'
207
208 """
209 return RequiresContext(lambda deps: dekind(function(self(deps)))(deps))
210
211 #: Alias for `bind_context` method, it is the same as `bind` here.
212 bind_context = bind

Callers

nothing calls this directly

Calls 2

dekindFunction · 0.90
RequiresContextClass · 0.85

Tested by

no test coverage detected