Browse files

Added a chain function for compiling a list of captured values.

  • Loading branch information...
1 parent 6626e9a commit 52a3cab4ff69cfc82ba139e98695f22e00dc453c @massung committed Sep 3, 2011
Showing with 8 additions and 0 deletions.
  1. +8 −0 src/parsec.erl
View
8 src/parsec.erl
@@ -15,6 +15,7 @@
-export([bind/2,
bind_/2,
return/1,
+ chain/2,
do/1,
parse/2,
pzero/0,
@@ -64,6 +65,13 @@ return (X) ->
(Error) -> Error
end.
+%% helper function
+chain_ ([P],Acc,F) -> bind(P,fun (X) -> F(Acc++[X]) end);
+chain_ ([P|PS],Acc,F) -> bind(P,fun (X) -> chain_(PS,Acc++[X],F) end).
+
+%% chain capture, passing a list of values to a function
+chain (PS,F) -> chain_(PS,[],F).
+
%% bind a list of combinators, ignoring all results except the last
do ([P]) -> bind(P,fun (X) -> return(X) end);
do ([P|PS]) ->

0 comments on commit 52a3cab

Please sign in to comment.