notes on returns

This commit is contained in:
Correl Roush 2024-04-03 22:19:24 -04:00
parent cbbfab2bb0
commit a1132ae416
1 changed files with 49 additions and 0 deletions

View File

@ -0,0 +1,49 @@
:PROPERTIES:
:ID: 4276e1de-1ac5-4e4d-896c-1601e2e61a4e
:END:
#+title: Returns
A library implementing various [[id:8ee5037f-0673-4968-9ec4-184bf31dd72d][Monads]] in [[id:cda9c620-fec5-4549-b979-22fc06819d77][Python]].
* Benefits
** Explicit errors
** Composition
*** Asynchronous composition
* Drawbacks
** Learning curve
** Formatting call chains in YAPF
Call chaining formats poorly in YAPF. Consider the following example which
performs the following steps:
1. Builds a request object
2. Wraps the request object in a Future
3. Binds the request to the HTTP send method
4. Binds the result to a response handler
The code, formatted by YAPF, looks like this:
#+begin_src python
return FutureResult.from_result(
self.build_request(request)).bind_async(safe_send).bind_result(
self.load_response)
#+end_src
The same code, formatted instead by the Black formatter, looks like this:
#+begin_src python
return (
FutureResult.from_result(self.build_request(request))
.bind_async(safe_send)
.bind_result(self.load_response)
)
#+end_src
A workaround is to use the =pointfree= module from =returns=, though this comes
with the additional overhead of understanding and using the point-free
functions.
#+begin_src python
return flow(
FutureResult.from_result(self.build_request(request)),
pointfree.bind_async(safe_send),
pointfree.bind_result(self.load_response),
)
#+end_src