From: Georg Bauhaus <rm-host.bauhaus@maps.futureapps.de>
Subject: Re: Iterators in Ada2012
Date: Wed, 09 Nov 2011 11:55:52 +0100
Date: 2011-11-09T11:55:53+01:00 [thread overview]
Message-ID: <4eba5c38$0$6560$9b4e6d93@newsspool4.arcor-online.net> (raw)
In-Reply-To: <j9cujd$g3k$1@dont-email.me>
On 11/9/11 5:12 AM, R. Tyler Croy wrote:
> Additionally, generator expressiones: `(x for x in iterable)` are lazily
> evaluated so you don't have to actually have your working set in memory
> all at once.
Maybe it is worth mentioning---though in this case savings come from the
other end where collections exist---that Iterate may not have to
inspect every element. Exceptions such as StopIteration in the following
example, can make the process stop early, as needed:
with Ada.Containers.Ordered_Maps;
with Ada.Text_IO;
procedure Iter is
type Location is digits 8;
type Coords is record
Sun : Location;
Snow : Location;
end record;
type Place is (Nowhere, Springfield, Shangri_La, Meryton, Zamunda);
package Maps is new Ada.Containers.Ordered_Maps
(Key_Type => Place,
Element_Type => Coords);
StopIteration : exception;
Whereabouts : Maps.Map;
First_Above_Equator : Place;
procedure Is_Above (Key : Place; Element : Coords) is
-- When place `Key` is above the equator, stores it in
-- `First_Above_Equator` and raises `StopIteration`
begin
if Element.Snow > 0.0 then
First_Above_Equator := Key;
raise StopIteration;
end if;
end Is_Above;
procedure Stops_Early (Position : Maps.Cursor) is
-- inspects key/element via `Is_Above`
begin
Maps.Query_Element (Position, Is_Above'Access);
end Stops_Early;
begin
First_Above_Equator := Nowhere;
Whereabouts.Insert (Meryton, Coords'(Sun => 0.0, Snow => 51.0));
-- ...
Whereabouts.Iterate (Stops_Early'Access);
exception
when StopIteration =>
Ada.Text_IO.Put_Line (Place'Image (First_Above_Equator));
end Iter;
> An Ada "built-in" equivalent of a generator expression I actually don't
> know of,
Me neither; though Ada.Numerics.*Random are examples of how
to model producing elements on demand. I imagine one can do similar
things with task objects in order to emulate a Python style yield.
The result, then, is a generating mechanism that does not require
a working set in memory. I guess one could make the mechanism
more generically useful.
task body Generator is
Current : Value_Type;
begin
loop
Current := Produce_Next;
accept Next (Result : out Value_Type) do
Result := Current;
end Next;
end loop;
end Generator;
And wrap it in an Ada.Container like package.
next prev parent reply other threads:[~2011-11-09 10:55 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-11-08 10:05 Iterators in Ada2012 comp.lang.php
2011-11-08 13:13 ` Georg Bauhaus
2011-11-08 13:42 ` Dmitry A. Kazakov
2011-11-09 4:12 ` R. Tyler Croy
2011-11-09 9:02 ` Simon Wright
2011-11-09 22:58 ` R. Tyler Croy
2011-11-10 8:07 ` Georg Bauhaus
2011-11-09 10:55 ` Georg Bauhaus [this message]
2011-11-25 14:20 ` Yannick Duchêne (Hibou57)
2011-11-08 21:25 ` Randy Brukardt
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox