[erlang-questions] Best practices -- conditional statements

Donald Steven t6sn7gt@REDACTED
Wed Feb 13 16:29:07 CET 2019


Aha!!!!!!!!!  Thanks Jesper and Ivan, much appreciated.

Don

On 2/13/2019 10.25 AM, Jesper Louis Andersen wrote:
> You can remove some of that repetition as well:
>
> notes(N) when N < 6  -> 8;
> notes(N) when N < 12 -> 4;
> notes(_N)            -> 2.
>
> makePanPositionL(Notes, rapidlyleft2right, L) -> 
> makePanPositionL(Notes, {rapidly, left2right}, L);
> makePanPositionL(Notes, rapidlyright2left, L) -> 
> makePanPositionL(Notes, {rapidly, right2left}, L);
> makePanPositionL(Notes, {rapidly, Mode}, L)   -> 2 * 
> makePanPositionL(Notes, Mode, L);
> makePanPositionL(Notes, left2right, [Cur|_])  -> Cur + notes(Notes);
> makePanPositionL(Notes, right2left, [Cur|_])  -> Cur - notes(Notes).
>
> Though this is golfing, so go for the solution you think is most easy 
> to read. One advantage of the above model is that it might be better 
> if you are toying with altering the scaling. The scaling parameters 
> are in one place in this code, so alteration is easier. Also, you can 
> simplify by using an erlang term rather than an atom via the {rapidly, 
> Mode} trick. Or perhaps just {scale, 2, Mode} and then interpret this as
>
> mpp(Notes, {scale, K, Mode}, L) ->
>   K * mpp(Notes, Mode, L).
>
>
> On Wed, Feb 13, 2019 at 4:06 PM Ivan Uemlianin <ivan@REDACTED 
> <mailto:ivan@REDACTED>> wrote:
>
>     Some people like it like that.  The repitition on the RHS makes me
>     think there's room for more streamlining.
>
>     On 13/02/2019 15:02, Donald Steven wrote:
>>     Alas, that creates a different form of code sprawl, as in:
>>
>>     makePanPositionL(Notes, left2right, [Cur|_]) whenNotes<6->Cur+8;
>>     makePanPositionL(Notes, left2right, [Cur|_]) whenNotes<12->Cur+4;
>>     makePanPositionL(Notes, left2right, [Cur|_]) ->Cur+2.
>>     makePanPositionL(Notes, right2left, [Cur|_]) whenNotes<6->Cur-8;
>>     makePanPositionL(Notes, right2left, [Cur|_]) whenNotes<12->Cur-4;
>>     makePanPositionL(Notes, right2left, [Cur|_]) ->Cur-2.
>>     makePanPositionL(Notes, rapidlyleft2right, [Cur|_])
>>     whenNotes<6->Cur+16;
>>     makePanPositionL(Notes, rapidlyleft2right, [Cur|_])
>>     whenNotes<12->Cur+8;
>>     makePanPositionL(Notes, rapidlyleft2right, [Cur|_]) ->Cur+4.
>>     makePanPositionL(Notes, rapidlyright2left, [Cur|_])
>>     whenNotes<6->Cur-16;
>>     makePanPositionL(Notes, rapidlyright2left, [Cur|_])
>>     whenNotes<12->Cur-8;
>>     makePanPositionL(Notes, rapidlyright2left, [Cur|_]) ->Cur-4.
>>
>>
>>     On 2/13/2019 8.35 AM, Jesper Louis Andersen wrote:
>>>     On Wed, Feb 13, 2019 at 1:51 PM Ivan Uemlianin <ivan@REDACTED
>>>     <mailto:ivan@REDACTED>> wrote:
>>>
>>>         This might be cheating but I would avoid the branching
>>>         altogether if
>>>         poss., eg:
>>>
>>>              PanPosition = CurrentPanPosition + notes_to_pad(Notes),
>>>              ...
>>>
>>>              notes_to_pad(N) where N <  6 -> 8;
>>>              notes_to_pad(N) where N < 12 -> 4;
>>>              notes_to_pad(_)              -> 2.
>>>
>>>
>>>     makePanPositionL(Notes, left2right, [Cur|_]) when Notes < 6  ->
>>>     Cur + 8;
>>>     makePanPositionL(Notes, left2right, [Cur|_]) when Notes < 12 ->
>>>     Cur + 4;
>>>     makePanPositionL(Notes, left2right, [Cur|_])                 ->
>>>     Cur + 2.
>>>
>>>     You can just lift everything into a pattern match for this piece
>>>     of code, which is what I think I'd do. It makes the result far
>>>     more tabular, which in some cases is easier to read. There may
>>>     be other reasons in the code to not do this, but as it was
>>>     written, I think I'd pick the above as the solution. YMMV of course.
>>>
>>>
>>>     _______________________________________________
>>>     erlang-questions mailing list
>>>     erlang-questions@REDACTED  <mailto:erlang-questions@REDACTED>
>>>     http://erlang.org/mailman/listinfo/erlang-questions
>>
>
>     -- 
>     ============================================================
>     Ivan A. Uemlianin PhD
>     Llaisdy
>
>     Ymchwil a Datblygu Technoleg Lleferydd
>     Speech Technology Research and Development
>
>                          ivan@REDACTED  <mailto:ivan@REDACTED>
>                              @llaisdy
>                               llaisdy.wordpress.com  <http://llaisdy.wordpress.com>
>                    github.com/llaisdy  <http://github.com/llaisdy>
>                           www.linkedin.com/in/ivanuemlianin  <http://www.linkedin.com/in/ivanuemlianin>
>
>                              festina lente
>     ============================================================
>
>
>
> -- 
> J.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20190213/06b4636f/attachment.htm>


More information about the erlang-questions mailing list