exported variables (was: RE: How to make GS Widgets)
Tony Rogvall
tony@REDACTED
Fri Apr 16 16:13:11 CEST 2004
fredagen den 16 april 2004 kl 08.22 skrev Cedric Shock:
> Richard O'Keefe and Ulf Wiger,
>
> Thank you for your suggestions of improved coding style. This is my
> first week of erlang programming, or even functional programming (I
> started by reading Concurrent Programming in Erlang a week ago). I am
> still strugling through documentation with almost everything I do. I
> like the style
>
> {Red,Green,Blue} = case SuppliedColor of
> {_,_,_} -> SuppliedColor;
> _ -> {0,0,0}
> end
>
> For its brevity and the way it makes the purpose of the code clear. I
> have been bothered by the weekness of my quards, but I do not know how
> to make them stronger. How can I put gaurds on the values of members
> of tuples? For example, I would like the SuppliedColor to be integers
> in the range [0, 255]. How could I write this guard clause? Is this
> the right thing to do?
>
> {Red,Green,Blue} = case SuppliedColor of
> {TestRed,TestGreen,TestBlue} when
> is_integer(TestRed),
> is_integer(TestGreen),
> is_integer(TestBlue),
> TestRed =< 255, TestRed => 0,
> TestGreen =< 255, TestGreen => 0,
> TestBlue =< 255, TestBlue => 0, ->
> SuppliedColor;
> _ ->
> {0,0,0}
> end
>
The above test is ok (except that you write >= not => in Erlang)
An other version I like is to check all at once :-) the band/bor/bnot
are
bitwise operators.
case SuppliedColor of
{R,G,B} when (R bor G bor B) band (bnot 255) == 0 ->
SuppliedColor;
_ ->
{0,0,0}
end.
/Tony
More information about the erlang-questions
mailing list