comp.lang.ada
 help / color / mirror / Atom feed
From: Christos Chryssochoidis <C.Chryssochoidis@gmail.com>
Subject: Re: Newbie's question
Date: Wed, 13 Feb 2008 13:06:04 +0200
Date: 2008-02-13T13:06:04+02:00	[thread overview]
Message-ID: <1202900672.56552@athprx03> (raw)
In-Reply-To: <1202856976.51300@athprx04>


 From a reply I received in private, it seems that the "generic" version 
of that code of mine is of interest for some, so I post it, making no 
claim that it is well written…







with Ada.Containers.Vectors;
with Ada.Text_IO;

procedure Test_Filter is

    subtype Index is Natural;
    type Int_Array is array(Index range<>) of Integer;


    generic
       with function Predicate(Element : Integer) return Boolean;
    function Filter(Elements : Int_Array) return Int_Array;


    function Filter(Elements : Int_Array) return Int_Array is
       package Int_Vectors is new Ada.Containers.Vectors(Element_Type => 
Integer, Index_Type => Index);
       Tmp_Vector : Int_Vectors.Vector;


    begin
       for I in Elements'Range loop
          if Predicate(Elements(I)) then
             Tmp_Vector.Append(Elements(I));
          end if;
       end loop;

   Copy_Vector:
       declare
          Result : Int_Array(0..Index(Tmp_Vector.Length)-1);
          I : Integer := 0;
          procedure Copy(Position: Int_Vectors.Cursor) is
          begin
             Result(I) := Int_Vectors.Element(Position);
             I := I + 1;
          end Copy;

       begin
          Tmp_Vector.Iterate(Copy'Access);

          return Result;
       end Copy_Vector;



    end Filter;

    function Greater_Equal_3(Element :Integer) return Boolean is
    begin
       return Element <= 3;
    end Greater_Equal_3;


    Int_Array1 : Int_Array := (1,2,3,4,5);
    Int_Array2 : Int_Array(1..3);
    function My_Filter is new Filter(Predicate=>Greater_Equal_3);
begin
    Int_Array2 := My_Filter(Int_Array1);
    for I in Int_Array1'Range loop
       Ada.Text_IO.Put_Line(Int_Array1(I)'Img);
    end loop;

    Ada.Text_IO.Put_Line("");

    for I in Int_Array2'Range loop
       Ada.Text_IO.Put_Line(Int_Array2(I)'Img);
    end loop;

end Test_Filter;



      reply	other threads:[~2008-02-13 11:06 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-02-11 14:31 Newbie's question Christos Chryssochoidis
2008-02-11 16:28 ` Newbie's question [SOLVED] Christos Chryssochoidis
2008-02-11 17:42 ` Newbie's question Jeffrey R. Carter
2008-02-12 22:56   ` Christos Chryssochoidis
2008-02-13 11:06     ` Christos Chryssochoidis [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox