datum
datum copied to clipboard
Document recursion flow with category/monads
E.g. How to implement following code using m_state
sync_github(#account{} = Account, Page) ->
case m_http:once( sync_github_at(Account, Page) ) of
{ok, []} ->
{ok, []};
{ok, Head} ->
case sync_github(Account, Page + 1) of
{ok, Tail} ->
{ok, Head ++ Tail};
{error, _} = Error ->
Error
end;
{error, _} = Error ->
Error
end.
or
sync_github(#account{} = Account, Page) ->
[m_state ||
Head <- sync_github_at(Account, Page),
Tail <- next(Head, Account, Page),
cats:unit(Head ++ Tail)
].
next([], _, _) ->
fun(State) -> [[] | State] end;
next(_, Account, Page) ->
sync_github(Account, Page + 1).