[erlang-questions] Erlang style question - input handling

t ty tty.erlang@REDACTED
Wed Sep 27 18:10:05 CEST 2006


I'm going to assume you terminate on the first failed test.

testAndDo(Data, [], Cont) ->
   Cont(Data);
testAndDo(Data, [H | FuncList], Cont) ->
   {Func, ErrCode} = H,
   case Func(Data) of
      true ->
         testAndDo(Data, FuncList, Cont);
      _ ->
         ErrCode
   end.

doSomething(D) ->
   D + 5.

Data = 3,
FuncList = [ {fun erlang:is_number/1, err1},
                  {fun erlang:is_port/1, err2},
                  {fun erlang:is_atom/1, err3}].
testAndDo(Data, FuncList, fun(D) -> doSomething(D) end)

You still pass data around but I suspect the recursive call would be
quite efficient in it.

t


On 9/27/06, Nohl Attila Rajmund <attila.rajmund.nohl@REDACTED> wrote:
> Hello!
>
> I have a couple of functions which follow this pattern:
> f(X, Y) ->
>      % collect some data
>      % do something with the collected data
>      .
>
> I need to add some checks based on the collected data and if the data is
> wrong, the "do something" part should not happen and an error code
> should be returned. There are a set of checks, different functions need
> different set of checks. In C/C++/Java I'd insert a code like this
> between the collecting part and the "do something" part:
>
> if (!check1(data1)) {
>      return ERROR_CODE1;
> }
> if (!check2(data2)) {
>      return ERROR_CODE2;
> }
> if (!check3(data3)) {
>      return ERROR_CODE3;
> }
>
> and then the rest of the function would be not touched. However, there
> is no return in Erlang so I can't do this. I've seen two approaches to
> implement it:
> case check1(Data1) of
>      true ->
>          case check2(Data2) of
>              true ->
>                  case check3(Data3) of
>                      true ->
>                          % do something
>                      false ->
>                          ?ERROR_CODE3
>                  end;
>              false ->
>                  ?ERROR_CODE2
>          end;
>      false ->
>          ?ERROR_CODE1
> end
>
> I really don't like this extra indentation, especially if "do something"
> also had a couple of indentations. The other approach is:
> f() ->
>      % collect some data
>      case check1(Data1) of
>          true ->
>              do_f(Lots_of_data);
>          false ->
>              ?ERROR_CODE1
>      end.
>
> do_f(Lots_of_data) ->
>      case check2(Data2) of
>          true ->
>              really_do_f(Lots_of_data);
>          false ->
>              ?ERROR_CODE2
>      end.
>
> really_do_f(Lots_of_data) ->
>      case check3(Data3) of
>          true ->
>              really_really_do_f(Lots_of_data);
>          false ->
>              ?ERROR_CODE3
>      end.
>
> really_really_do_f(Lots_of_data) ->
>      %do something
>
>
> My problem is with this approach that I run out of meaningful function
> names pretty soon and might have to pass a lots of data around. Is
> there a more elegant solution to this kind of problem?
>
>                                 Bye,NAR
> --
> "Beware of bugs in the above code; I have only proved it correct, not
> tried it."
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list