From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,f3405cf75879a8dc X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Path: g2news1.google.com!news2.google.com!postnews.google.com!t8g2000yqk.googlegroups.com!not-for-mail From: Alex Mentis Newsgroups: comp.lang.ada Subject: Re: basic questions on using Ada arrays Date: Fri, 8 Oct 2010 01:02:14 -0700 (PDT) Organization: http://groups.google.com Message-ID: <7e397a82-e958-4f66-aa03-cd963272c026@t8g2000yqk.googlegroups.com> References: NNTP-Posting-Host: 46.42.104.65 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1286565068 19711 127.0.0.1 (8 Oct 2010 19:11:08 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Fri, 8 Oct 2010 19:11:08 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: t8g2000yqk.googlegroups.com; posting-host=46.42.104.65; posting-account=CedHywoAAAAcVQwJt5x8TeyAwJA5ElaR User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10 ( .NET CLR 3.5.30729; .NET4.0E),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:14446 Date: 2010-10-08T01:02:14-07:00 List-Id: On Oct 6, 6:43=A0pm, "Nasser M. Abbasi" wrote: > Ada experts: > > I am really rusty with Ada. Wanted to find if I apply a function to an > array in one call without having to loop calling the function for each > entry in the array? > > Suppose I have an array of values, and I want to take the sin() of each > value in the array? > > This below does not work, last statement is wrong > > --------------------------------------- > with ada.text_io; use ada.text_io; > with Ada.Numerics; use =A0Ada.Numerics; > with Ada.Numerics.Elementary_Functions; > use =A0Ada.Numerics.Elementary_Functions; > with Ada.float_Text_IO; use Ada.float_Text_IO; > > procedure test2 =A0is > =A0 =A0 nPoints : constant :=3D100; > =A0 =A0 del : float :=3D 2.0*Pi/float(nPoints-1); > =A0 =A0 x =A0 : array(1..nPoints) of float; > =A0 =A0 y =A0 : array(1..nPoints) of float; > begin > > =A0 =A0 x(1):=3D0.0; > =A0 =A0 for i in 2..nPoints loop > =A0 =A0 =A0 =A0x(i) =A0:=3D x(i-1)+del; > =A0 =A0 end loop; > > =A0 =A0 x:=3Dsin(x(1..nPoints)); > > end test2; > ------------------------------------ > > Also, is there a way to initialize an array using a formula or a > function call? I know I can do > > =A0 x =A0 : array(1..nPoints) of float :=3D (others =3D> 0.0) > > But I wanted to make each entry in the array to have some value > depending on the index value. =A0I did not know how, so that is what the > above small loop does, to initialize x array. > > It would be nice to have been to do this initialization at declaration ti= me. > > The bigger question really, is if one map functions on array in Ada, or > do operations on array in one call, say like multiply a constant by > array, without using loops all the time. loops are so out of fashion > these days :) > > Something like this in Fortran > > ---------------------- > program main > =A0 =A0 integer :: x(10)=3D1 ; > =A0 =A0 x =3D 5 * x; > end program > ----------------------- > > compiles, runs ok, but The Ada code > > ---------------------- > procedure test3 =A0is > =A0 =A0 x =A0 : array(1..10) of integer :=3D(others=3D>1); > begin > =A0 =A0 x:=3D 5 * x; > end test3; > --------------- > > $ gnatmake test3.adb > test3.adb:4:12: expected type universal integer > test3.adb:4:12: found type of x declared at line 2 > > I undertand exactly the error and why. My question is how to code it in > Ada to the same, without using loops? > > thanks, > --Nasser You can write your own. I'm in a hurry, so I don't have time to tweak this the way I'd like...I'd want to try to do this with a generic array type and with more flexible signatures for the functions/ procedures to be mapped, but my general idea is below. Putting it in a package would be more flexible. You could probably gin up the code to do a mapped initialization as well...but not sure. Haven't given it much thought at the moment. Alex ---------------------------------------------------------------------------= ----- -- File: Ada_Map.adb -- -- Created on Oct 8, 2010 ---------------------------------------------------------------------------= ----- -- -- Description of Ada_Map -- -- @author alexander.mentis -- with Ada.Text_IO, Ada.Numerics.Elementary_Functions; use Ada.Text_IO, Ada.Numerics.Elementary_Functions; procedure Ada_Map is type Array_Type is array (1 .. 10) of Float; function Map (F : not null access function (N : Float) return Float; A : Array_Type) return Array_Type is Result : Array_Type; begin -- Map for I in A'Range loop Result (I) :=3D F (A (I)); end loop; return Result; end Map; procedure Map (P : not null access procedure (S : String); A : Array_Type) is begin -- Map for I in A'Range loop P (A (I)'Img); end loop; end Map; X : Array_Type :=3D (others =3D> 5.0); begin -- Ada_Map X :=3D Map (Sin'Access, X); Map(Put_Line'Access, X); end Ada_Map;