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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham 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!news3.google.com!feeder.news-service.com!news.netcologne.de!newsfeed-fusi2.netcologne.de!newsfeed.straub-nv.de!nuzba.szn.dk!news.jacob-sparre.dk!pnx.dk!not-for-mail From: Jacob Sparre Andersen Newsgroups: comp.lang.ada Subject: Re: basic questions on using Ada arrays Date: Wed, 06 Oct 2010 18:22:18 +0200 Organization: Jacob Sparre Andersen Message-ID: <871v838f5x.fsf@hugsarin.sparre-andersen.dk> References: NNTP-Posting-Host: 95.209.237.223.bredband.3.dk Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Trace: munin.nbi.dk 1286382145 10086 95.209.237.223 (6 Oct 2010 16:22:25 GMT) X-Complaints-To: news@jacob-sparre.dk NNTP-Posting-Date: Wed, 6 Oct 2010 16:22:25 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux) Cancel-Lock: sha1:YFDKlxdJzXkl930y6ZOb8Hx7Qiw= Xref: g2news1.google.com comp.lang.ada:14415 Date: 2010-10-06T18:22:18+02:00 List-Id: Nasser M. Abbasi wrote: > 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? Only if the function/operation is declared for the array type. The operator "and" is automatically declared for arrays of Booleans. > Suppose I have an array of values, and I want to take the sin() of > each value in the array? The function "Sin" is not declared automatically for arrays of Floats. You will have to declare it yourself: function Sin (Item : in The_Array_Type) return The_Array_Type is begin return Result : The_Array_Type do for I in Item'Range loop Result (I) := Sin (Item (I)); end loop; end return; end Sin; > But I wanted to make each entry in the array to have some value > depending on the index value. You have to use a loop to do that (you may of course hide the loop in a function). > 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 :) There are generally no predefined mathematical operations on arrays of scalars in Ada. You could propose it to the ARG. They might even accept it. > Something like this in Fortran > > ---------------------- > program main > integer :: x(10)=1 ; > x = 5 * x; > end program > ----------------------- > > compiles, runs ok, but The Ada code > > ---------------------- > procedure test3 is > x : array(1..10) of integer :=(others=>1); > begin > x:= 5 * x; > end test3; > --------------- ... should be: procedure Test_3 is type Demo_Array is array (1 .. 10) of Integer; function "*" (Left : Integer; Right : Demo_Array) return Demo_Array is begin return Result : Demo_Array do for I in Right'Range loop Result (I) := Left * Right (I); end loop; end return; end "*"; X : Demo_Array := (others => 1); begin X := 5 * X; end Test_3; Jacob -- Jacob Sparre Andersen Research & Innovation Vesterbrogade 148K, 1. th. 1620 K�benhavn V Danmark