datum icon indicating copy to clipboard operation
datum copied to clipboard

Document recursion flow with category/monads

Open fogfish opened this issue 6 years ago • 0 comments

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).

fogfish avatar Apr 23 '19 19:04 fogfish