From: "Alex Mentis" <foo@invalid.invalid>
Subject: Re: Callback in Ada
Date: Sat, 27 Nov 2010 10:26:42 +0000 (UTC)
Date: 2010-11-27T10:26:42+00:00 [thread overview]
Message-ID: <icqmd1$guj$1@news.eternal-september.org> (raw)
In-Reply-To: 8lc2d0Fb6jU1@mid.individual.net
Georg Maubach wrote:
> Hi All,
>
> today I learnt what a callback function is and also how it is used in
> Python. I have tested the following code which runs in Python:
>
> def apply_to (function, valueList):
> resultList = []
>
> if __debug__: print "Calling function ", function
>
> for value in valueList:
> if __debug__: print "value: ", value
> r = function (value)
> if __debug__: print "result: ", r
> resultList.append(r)
>
> return resultList
>
> def dublicate (value):
> return (value * 2)
>
> def power (value):
> return (value * value)
>
> valueList = [1, 2, 3, 4, 5]
>
> double_values = apply_to(dublicate, valueList)
> power_values = apply_to(power, valueList)
>
> print "Values doubled: ", double_values
> print "Values powered: ", power_values
>
> Reference: http://en.wikipedia.org/wiki/Callback_(computer_science)
>
> If put into a file called "callback-test-in-python.py" and called
> python -O callback-test-in-python.py it will deliver the output
>
> Values doubled: 2, 4, 6, 8, 10
> Values powered: 1, 4, 9, 16, 25
>
> How would I do that in Ada?
>
> Regards
>
> George
Basically the same idea as Dimitry, except I use functions so the code
works more similarly to your Python:
with Ada.Text_IO, Ada.Integer_Text_IO;
use Ada.Text_IO, Ada.Integer_Text_IO;
procedure Callback is
type Int_Array is array (1..5) of Integer;
function Apply_To
(Func : not null access function (Value : Integer) return
Integer;
Value_List : Int_Array) return Int_Array is
Result : Int_Array;
begin -- Apply_To
for Value in Value_List'Range loop
Result (Value) := Func (Value_List(Value));
end loop;
return Result;
end Apply_To;
function Dublicate (Value : Integer) return Integer is
begin -- Dublicate
return Value * 2;
end Dublicate;
Initial : Int_Array := (1, 2, 3, 4, 5);
Result : Int_Array;
begin
Result := Apply_To (Dublicate'Access, Initial);
Put("Values doubled: ");
for Value in Result'Range loop
Put (Result (Value), 0);
if Value /= Result'Last then
Put (", ");
end if;
end loop;
end Callback;
next prev parent reply other threads:[~2010-11-27 10:26 UTC|newest]
Thread overview: 36+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-11-27 9:47 Callback in Ada Georg Maubach
2010-11-27 10:11 ` Dmitry A. Kazakov
2010-11-27 10:22 ` Ludovic Brenta
2010-11-27 11:04 ` Vinzent Hoefler
2010-11-27 22:05 ` Maciej Sobczak
2010-11-27 10:26 ` Alex Mentis [this message]
2010-11-27 10:46 ` Vinzent Hoefler
2010-11-27 11:32 ` Georg Bauhaus
2010-11-27 21:11 ` Jeffrey Carter
2010-11-27 21:20 ` Dmitry A. Kazakov
2010-11-28 21:35 ` Maciej Sobczak
2010-11-29 8:41 ` Dmitry A. Kazakov
2010-11-29 10:12 ` Maciej Sobczak
2010-11-29 11:04 ` Dmitry A. Kazakov
2010-11-30 1:32 ` Randy Brukardt
2010-11-30 8:38 ` Dmitry A. Kazakov
2010-11-30 9:14 ` AdaMagica
2010-11-30 12:37 ` Georg Bauhaus
2010-11-30 20:28 ` Randy Brukardt
2010-11-30 20:31 ` Randy Brukardt
2011-01-01 16:46 ` Yannick Duchêne (Hibou57)
2011-01-02 10:14 ` AdaMagica
2011-01-02 14:11 ` Robert A Duff
2011-01-02 15:15 ` AdaMagica
2011-01-02 15:28 ` Robert A Duff
2011-01-02 15:38 ` Alex Mentis
2011-01-02 15:44 ` Robert A Duff
2011-01-03 21:38 ` Randy Brukardt
2011-01-04 0:11 ` Robert A Duff
2011-01-04 18:33 ` Alex Mentis
2011-01-04 19:47 ` Robert A Duff
2011-01-04 20:21 ` Randy Brukardt
2011-01-04 20:15 ` Randy Brukardt
2010-11-29 15:24 ` Callback in Ada (User Data argument) Warren
2010-11-30 1:35 ` Randy Brukardt
2010-11-30 16:51 ` Warren
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox