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,de7dd126d6737f3a X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news3.google.com!feeder.news-service.com!85.214.198.2.MISMATCH!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: "Alex Mentis" Newsgroups: comp.lang.ada Subject: Re: Callback in Ada Date: Sat, 27 Nov 2010 10:26:42 +0000 (UTC) Organization: A noiseless patient Spider Message-ID: References: <8lc2d0Fb6jU1@mid.individual.net> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Injection-Date: Sat, 27 Nov 2010 10:26:42 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="Q5trZBwg1Y0nqPCurq0BRg"; logging-data="17363"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/gLgqLXkZRLrW+svmCoFtw9jl1MsKUd7A=" User-Agent: XanaNews/1.19.1.269 Cancel-Lock: sha1:f/GF/l7sfAPJEbo76DFfpCtSWIA= Xref: g2news2.google.com comp.lang.ada:16646 Date: 2010-11-27T10:26:42+00:00 List-Id: 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;