Migrate from `State` to `Maybe`
Closed this issue · 0 comments
katunilya commented
State
s are nice, but they make code too complex I believe and make some mismatch as currently State
instead of its classical implementation of (state, value)
pair is some mix of Safe
and Result
monads.
In this way it seems much more profitable to use Maybe
monad (#4) in combination with Future
monad just to keep everything nice and clean.
OLD
Based on Giraffe project I see that it makes sense to get rid of complex State
management in favor of Middleware-like continuation style.
There would be 3 core function types:
import typing
# function that accepts context changes something about it and returns context
ContextFunc = typing.Callable[[Context], Future[Maybe[Context]]]
# this is a handler. Handler = application (must be composable, curried)
ContextHandler = typing.Callable[[ContextFunc, Context], Future[Maybe[Context]]]
Why handler returnsFuture[Maybe[Context]]
?
Each handler is a sync function, but it might have some async operations inside. Async operations should be performed usingFuture
.Maybe
is needed for railroad-oriented basis - when we haveSome[Context]
than everything is OK and we go next, but when Context is actuallyNothing
than the way we went is wrong and we need to pick some other handling option.
Tasks
Get rid ofState
monad usage (do not delete, but do not use) Think how to restructure it;Provide base typesContextFunc
andContextHandler
(possibly shortcut forFuture[Maybe[Context]]
;ProvideContextHandler
composition via<<
(__lshift__
) operator - class wrapper + decorator;ImproveFuture
's>>
(bind
) for Future returning function (might need complete redesign);Rework existing handlers toContextFunc
andContextHandler
;Rework existing tests;