Skip to content
This repository has been archived by the owner on Aug 30, 2019. It is now read-only.

Commit

Permalink
Reimplement sequence so that the monadic combinators are applied left…
Browse files Browse the repository at this point in the history
… to right, in addition to getting the order of the elements in the resultant list correct (closes github issue #5).
  • Loading branch information
Matthew Sackman committed Jan 12, 2012
1 parent 9620ecf commit 5d6dddd
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/monad.erl
Expand Up @@ -39,8 +39,10 @@ join(Monad, X) ->
Y]).

sequence(Monad, Xs) ->
lists:foldr(fun (X, Acc) ->
do([Monad || E <- X,
Es <- Acc,
return([E|Es])])
end, Monad:return([]), Xs).
sequence(Monad, Xs, []).

sequence(Monad, [], Acc) ->
do([Monad || return(lists:reverse(Acc))]);
sequence(Monad, [X|Xs], Acc) ->
do([Monad || E <- X,
sequence(Monad, Xs, [E|Acc])]).

0 comments on commit 5d6dddd

Please sign in to comment.