Slide 3
Slide 3 text
Example code
-spec repeat( FunName :: atom() | function(),
Args :: [ term() ],
Count :: pos_integer() ) ->
Result :: term() | { error, Reason :: term() }.
%% @doc This function repeats calls to a function name which may fail
%% given by the FunName parameter for Count number of times.
repeat(_FunName, _Args, 0) -> {error, count_reached_zero);
repeat(FunName, Args, Count) when is_atom(FunName) ->
do_repeat({?MODULE, FunName}, Args, Count);
repeat(FunName, Args, Count) when is_function(FunName) ->
do_repeat(FunName, Args, Count);
repeat(Other, _Args, _Count) -> erlang:error({error, badarg}, [Other, Args]).
do_repeat(Fun, Args, C) ->
try
erlang:apply(Fun, Args)
catch
_:_ -> repeat(FunName, Args, C – 1)
end.