Page 1 of 1

List::map and list:forall - shall I forget them?

Posted: 17 Sep 2015 7:36
by Ferenc Nagy
The new VIP introduced

Code: Select all

foreach and [ || ]
structures.
The

Code: Select all

findall, list::map and list:forall
are earlier language features.
Shall I forget them and use

Code: Select all

foreach and [ || ]
in the future?

Posted: 17 Sep 2015 18:16
by Thomas Linder Puls
You should forget findall, but the other ones may still be useful/preferred in some contexts.

Posted: 18 Sep 2015 6:20
by Peter Muraya
Frank,
My simple understanding is that the list comprehension, [||], replaced findall.

list::map and list::forall are additions that if they commonly appear in your code they are neater to use (and understand) than the equivalent coded using foreach. See the following examples....
...using list::map

Code: Select all

.... Z=list::map(Xs, Y), ...
... and not using list::map

Code: Select all

... C=varM:new(), foreach X in Xs do      C:value:=list::append(C:value, Y(X)) end foreach, Z=C:value, ...

Posted: 18 Sep 2015 8:00
by Thomas Linder Puls
It would be more natural to use list comprehension to map (and foreach to do forall):

Code: Select all

    % map     Ma = map(L, f),     Mb = [ f(X) || X in L],     % forall     forall(L, p),     foreach Y in L do         p(Y)     end foreach
If the function you map with and the predicate you "forall" with are existing as named entities in your code then the use of the map and forall predcates are quite simple and easy to read.

But if the code is more complex and you for example need to use anonymous predicates then the list comprehension and foreach versions may be prefarable.

Code: Select all

        Z = q(...),         % map         Ma =             map(L,                 { (X) = f(V, Z) :-                     V1 = g(Z, 12),                     V2 = h(V1)                 }),         Mb =             [ f(V, Z) ||                 X in L,                 V1 = g(Z, 12),                 V2 = h(V1)             ],         % forall         forall(L,             { (Y) :-                 V1 = a(Y, Z),                 V2 = b(Y, V1),                 p(V2)             }),         foreach Y in L do                 V1 = a(Y, Z),                 V2 = b(Y, V1),                 p(V2)         end foreach.