Search code examples
sumrangerascal

calculating sum of ListRelation range


I have a ListRelation called PLOCs of type:

lrel[loc, int] PLOCs = [<a, calcPLOC(a)> | a <- files];

where files is a set of locations, and calcPLOC calculates an integer based on that location.

Now I want the sum of all the calculated integers. I used 3 different ways to calculate this, and got 2 different answers:

1:

total = 0;
for (<a, b> <- PLOCs) { 
    total += b;
}
println("total PLOC: <total>"); // returns 23805

2:

total = sum(range(PLOCs));
println("total PLOC: <total>"); // returns 21313

3:

total = (0 | it + b | <a, b> <- PLOCs);
println("total PLOC: <total>"); // returns 23805

Why does the second method return a different result?


Solution

  • The range function from ListRelation "squeezes" out duplicate entries, but keeps their order.