Explorar el Código

Improved type checking in primitive constructors.

Paul Downen hace 13 años
padre
commit
012069a855
Se han modificado 1 ficheros con 25 adiciones y 18 borrados
  1. 25 18
      src/ebb_prim.erl

+ 25 - 18
src/ebb_prim.erl

@@ -2,8 +2,7 @@
 -include("../include/ebb_prim.hrl").
 
 %%% Operation construction
--export([func/1, func/2,
-	 value/1, values/1,
+-export([func/1, value/1,
 	 pipe/1, par/1,
 	 route/2, sync/1,
 	 split/1, merge/1,
@@ -23,34 +22,40 @@ func(F) when is_function(F) ->
     {arity, N} = erlang:fun_info(F, arity),
     #func{in=N, code=F}.
 
-func(F, N) when is_function(F), is_number(N) ->
-    pipe([func(F), split(N)]).
-
 value(X) -> #value{value=X}.
 
-values(Xs = [_|_]) ->
-    par([ value(X) || X <- Xs ]).
-
 pipe(Ops = [First|Rest]) ->
     try 
-	lists:foldl(fun(Op2, Op1) -> case can_connect(Op1, Op2) of
+	lists:foldl(fun(Op2, Op1) -> case is_operation(Op2) andalso
+					 can_connect(Op1, Op2) of
 					 true  -> Op2;
-					 false -> throw(bad_connection)
+					 false -> throw(badarg)
 				     end
 		    end,
-		    First, Rest)
+		    case is_operation(First) of
+			true  -> First;
+			false -> throw(badarg)
+		    end,
+		    Rest)
     of
 	Last -> #pipe{in=in_arity(First), out=out_arity(Last), ops=Ops}
     catch
-	throw:bad_connection -> erlang:error(badarg, [Ops])
+	throw:badarg -> erlang:error(badarg, [Ops])
     end.
 
 par(Ops = [_|_]) ->
+    case lists:all(fun is_operation/1, Ops) of
+	true  -> ok;
+	false -> erlang:error(badarg, [Ops])
+    end,
     {In, Out} = flatten_arity(Ops),
     #par{in=In, out=Out, ops=Ops}.
 
-route(N, Map) ->
-    Good = lists:all(fun(Source) -> Source > 0 andalso Source =< N end, Map),
+route(N, Map) when is_number(N) ->
+    Good = lists:all(fun(Source) ->is_number(Source) andalso
+				       Source > 0 andalso Source =< N
+		     end,
+		     Map),
     case Good of
 	true  -> #route{in=N, out=length(Map), map=Map};
 	false -> erlang:error(badarg, [N, Map])
@@ -65,10 +70,12 @@ merge(N) when is_number(N) -> #merge{size=N}.
 switch(Map = [{_Tag1, Op1}|Rest]) ->
     In = in_arity(Op1),
     Out = out_arity(Op1),
-    Match = lists:all(fun({_T_I, Op}) ->
-			      in_arity(Op) == In andalso out_arity(Op) == Out
-		      end,
-		      Rest),
+    Match = lists:all(
+	      fun({_T_I, Op}) ->
+		      is_operation(Op) andalso
+			  in_arity(Op) == In andalso out_arity(Op) == Out
+	      end,
+	      Rest),
     case Match of
 	true  -> #switch{in=1+In, out=Out, map=Map};
 	false -> erlang:error(badarg, [Map])