From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: basic questions on using Ada arrays
Date: Sat, 9 Oct 2010 01:35:37 -0500
Date: 2010-10-09T01:35:37-05:00 [thread overview]
Message-ID: <i8p2fr$11s$1@munin.nbi.dk> (raw)
In-Reply-To: 871v80g2s7.fsf@hugsarin.sparre-andersen.dk
"Jacob Sparre Andersen" <sparre@nbi.dk> wrote in message
news:871v80g2s7.fsf@hugsarin.sparre-andersen.dk...
> Randy Brukardt wrote;
>
>> If you're using an Ada 2005, [...]
>
...
> Wouldn't this implementation of the function be more elegant?
>
> function Apply (Func : access function (F : Float) return Float;
> Arr : in Float_Array) return Float_Array is
> begin
> return Result : Float_Array (Arr'Range) do
> Result (I) := Func (Arr (I));
> end return;
> end Apply;
It's too elegant: you forgot the for loop! (Thus, there is no declaration
for I). It should be:
function Apply (Func : access function (F : Float) return Float;
Arr : in Float_Array) return Float_Array is
begin
return Result : Float_Array (Arr'Range) do
for I in Arr'Range loop
Result (I) := Func (Arr (I));
end loop;
end return;
end Apply;
Using the extended return does simplify the code and possibly improves the
performance as well (by eliminating a copy of the array).
Randy.
next prev parent reply other threads:[~2010-10-09 6:35 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
2010-10-08 14:47 ` Jacob Sparre Andersen
2010-10-09 6:35 ` Randy Brukardt [this message]
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