comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: basic questions on using Ada arrays
Date: Thu, 7 Oct 2010 19:04:00 -0500
Date: 2010-10-07T19:04:00-05:00	[thread overview]
Message-ID: <i8ln5h$o0p$1@munin.nbi.dk> (raw)
In-Reply-To: ln8w2apy0u.fsf@nuthaus.mib.org

"Keith Thompson" <kst-u@mib.org> wrote in message 
news:ln8w2apy0u.fsf@nuthaus.mib.org...
...
> A more "generic" solution (though it doesn't use generics) is:

If you're using an Ada 2005, this solution is better using an anonymous 
access type, as in that case you can pass any matching subprogram, not just 
one from the same level. (If you don't do this, this will be annoyingly hard 
to use.) That is, change as follows:

> with Ada.Text_IO; use Ada.Text_IO;
> with Ada.Numerics; use Ada.Numerics;
> with Ada.Numerics.Elementary_Functions;
> use  Ada.Numerics.Elementary_Functions;
> with Ada.Float_Text_IO; use Ada.Float_Text_IO;
>
> procedure Test2 is
>   type Func_Ptr is access function(F: Float) return Float;

Delete this type.

>   type Float_Array is array(positive range <>) of Float;
>
>   function Apply(Func: Func_Ptr; Arr: Float_Array) return Float_Array is

Change this declaration to:

function Apply(Func: access function(F: Float) return Float; Arr: 
Float_Array) return Float_Array is

>      Result: Float_Array(Arr'Range);
>   begin
>      for I in Result'Range loop
>         Result(I) := Func(Arr(I));
>      end loop;
>      return Result;
>   end Apply;
>
>   function Init return Float_Array is
>      nPoints : constant := 100;
>      del : constant Float := 2.0*Pi/Float(nPoints-1);
>      Result: Float_Array(1 .. nPoints);
>   begin
>      for I in Result'Range loop
>         Result(I) := Float(I - 1) * del;
>      end loop;
>      return result;
>   end Init;
>
>   X: constant Float_Array := Init;
>   Y: constant Float_Array := Apply(sin'Access, X);
> begin
>   for I in X'Range loop
>      Put(X(I), Exp => 0);
>      Put(" => ");
>      Put(Y(I), Exp => 0);
>      New_Line;
>   end loop;
> end Test2;
>
> [...]


                       Randy.





  reply	other threads:[~2010-10-08  0:04 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-10-06 15:43 basic questions on using Ada arrays Nasser M. Abbasi
2010-10-06 16:17 ` Pascal Obry
2010-10-06 16:22 ` Jacob Sparre Andersen
2010-10-07  1:55   ` Keith Thompson
2010-10-08  0:04     ` Randy Brukardt [this message]
2010-10-08 14:47       ` Jacob Sparre Andersen
2010-10-09  6:35         ` Randy Brukardt
2010-10-26  2:20           ` Yannick Duchêne (Hibou57)
2010-10-06 17:03 ` Jeffrey Carter
2010-10-06 19:54 ` Simon Wright
2010-10-08  8:02 ` Alex Mentis
replies disabled

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