[m-users.] inline / anonymous predicates ... style...

Zoltan Somogyi zoltan.somogyi at runbox.com
Sun Mar 6 07:18:51 AEDT 2022


2022-03-06 00:10 GMT+11:00 "Sean Charles (emacstheviking)" <objitsu at gmail.com>:
> Hi,
> 
> Having recently seen the `errors.m` contribution to the Mercury extras folder, and more reading if compiler and mmc-doc source code, I often see this style:
> 
> P = (pred (...) .......
>       ),
>       list.take_while(P, ....
> or   list.foldl(P, ...
> 
> and I wondered is it a style matter or is the compiler able to do more if I define the predicate first ? I ask as my code for my transpiler rarely does this, mainly because of my past experience with other languages where you just throw the code inline:
> 
>     list.take(
> 	(pred(...)
>         ),
> 	..args..
>     )
> 
> is there a preferred style and if so, what was the basis for that decision ?

I tend to use the first style for three reasons.

- It gives a name to the lambda, and this name is useful documentation
  to readers of the code. This is by a significant margin the most important
  reason.

- It clearly separates the code of the lambda from the other arguments
  of the call.

- It reduces the indentation level of the code of the lambda by one.

If the code of the lambda is really short and obvious, such as
"func(X) = yes(X)", then none of these reasons apply, and in such cases,
I would probably use the second style.

Zoltan.


More information about the users mailing list