[m-users.] inline / anonymous predicates ... style...
Sean Charles (emacstheviking)
objitsu at gmail.com
Sun Mar 6 19:47:47 AEDT 2022
Good points... and I am now about two-thirds the way of refactoring all such code to use that style, I have only found a single case so far where the predicate is trivially small and thus deemed exempt, this one:
% Convert list of ps() strings to plain strings.
%
to_strings(In) = Out :-
list.map((pred(ps(_, S)::in, S::out) is det), In, Out).
where
% A positioned string.
% The position is the lexxer offset of the string in the source buffer.
%
:- type ps
---> ps(
psloc::location,
psbody::string
).
Thanks, and as I was performing the clean up, I did notice the decrease of the indent level, given my recent question about that I could but not smile to myself with a small amount of chagrin.
Sean.
> On 5 Mar 2022, at 20:18, Zoltan Somogyi <zoltan.somogyi at runbox.com> wrote:
>
>
> 2022-03-06 00:10 GMT+11:00 "Sean Charles (emacstheviking)" <objitsu at gmail.com <mailto: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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurylang.org/archives/users/attachments/20220306/aa169c79/attachment.html>
More information about the users
mailing list