katunilya/moona

Migrate from `State` to `Maybe`

Closed this issue · 0 comments

States 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 returns Future[Maybe[Context]]?
    Each handler is a sync function, but it might have some async operations inside. Async operations should be performed using Future. Maybe is needed for railroad-oriented basis - when we have Some[Context] than everything is OK and we go next, but when Context is actually Nothing than the way we went is wrong and we need to pick some other handling option.

Tasks

  • Get rid of State monad usage (do not delete, but do not use) Think how to restructure it;
  • Provide base types ContextFunc and ContextHandler (possibly shortcut for Future[Maybe[Context]];
  • Provide ContextHandler composition via << (__lshift__) operator - class wrapper + decorator;
  • Improve Future's >> (bind) for Future returning function (might need complete redesign);
  • Rework existing handlers to ContextFunc and ContextHandler;
  • Rework existing tests;