Loops/With multiple ranges: Difference between revisions

Content added Content deleted
Line 904: Line 904:
<pre>sum = 348173
<pre>sum = 348173
prod = -793618560</pre>
prod = -793618560</pre>
=={{header|Eiffel}}==
Eiffel does not support multiple ranges in the same fashion as PL/I. However, it does have an across loop, which does the trick, together with an inline agent (lambda function).
<lang eiffel>
class
APPLICATION

create
make

feature

prod, sum, x, y, z, one, three, seven: INTEGER

make
local
process: PROCEDURE
do
prod := 1; x := 5; y := -5; z := -2; one := 1; three := 3; seven := 7
process := (agent (j: INTEGER)
do
print (j.out + ", ")
sum := sum + j.abs
if prod.abs < 2^27 and j /= 0 then
prod := prod * j
end
end)

across (-three |..| (3^3).truncated_to_integer).new_cursor + (three - 1) as ic loop process.call (ic.item) end
across (-seven |..| seven).new_cursor + (x - 1) as ic loop process.call (ic.item) end
across 555 |..| (550 - y) as ic loop process.call (ic.item) end
across (-26 |..| 22).new_cursor + (three - 1) as ic loop process.call (ic.item) end
across 1927 |..| 1939 as ic loop process.call (ic.item) end
across (y |..| x).new_cursor + (-z - 1) as ic loop process.call (ic.item) end
across (11^x).truncated_to_integer |..| ((11^x).truncated_to_integer + 1) as ic loop process.call (ic.item) end

print ("%N")
print ("sum = " + sum.out + "%N") -- sum = 348,173
print ("prod = " + prod.out + "%N") -- prod = -793,618,560
end

end
</lang>
{{out}}
<pre>
sum= 348,173
prod= -793,618,560
</pre>

=={{header|Factor}}==
=={{header|Factor}}==
Factor doesn't have any special support for this sort of thing, but we can store iterable <code>range</code> objects in a collection and loop over them.
Factor doesn't have any special support for this sort of thing, but we can store iterable <code>range</code> objects in a collection and loop over them.