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,703c4f68db81387d X-Google-Thread: 109fba,703c4f68db81387d X-Google-Attributes: gid103376,gid109fba,public X-Google-Language: ENGLISH,UTF8 Path: g2news1.google.com!news4.google.com!newsfeed2.dallas1.level3.net!news.level3.com!newsfeed-00.mathworks.com!news.tele.dk!news.tele.dk!small.news.tele.dk!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsfeed.arcor.de!news.arcor.de!not-for-mail Date: Thu, 10 Mar 2005 15:25:32 +0100 From: Georg Bauhaus User-Agent: Debian Thunderbird 1.0 (X11/20050116) X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada,comp.lang.c++ Subject: Re: Teaching new tricks to an old dog (C++ -->Ada) References: <4229bad9$0$1019$afc38c87@news.optusnet.com.au> <1110032222.447846.167060@g14g2000cwa.googlegroups.com> <871xau9nlh.fsf@insalien.org> <3SjWd.103128$Vf.3969241@news000.worldonline.dk> <87r7iu85lf.fsf@insalien.org> <1110052142.832650@athnrd02> <1110284070.410136.205090@o13g2000cwo.googlegroups.com> <395uqaF5rhu2mU1@individual.net> <1110329098.642196@athnrd02> <1110361741.551255@athnrd02> <422edaec$0$26554$9b4e6d93@newsread4.arcor-online.net> <422f0e39$0$26546$9b4e6d93@newsread4.arcor-online.net> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Message-ID: <423058aa$0$26547$9b4e6d93@newsread4.arcor-online.net> Organization: Arcor NNTP-Posting-Date: 10 Mar 2005 15:24:42 MET NNTP-Posting-Host: de7907cb.newsread4.arcor-online.net X-Trace: DXC=HW=k_\Th6OSD__2dTlB=E[:ejgIfPPldTjW\KbG]kaMXGSi?jHD8GOPLkNlRKiA?OXhP3YJKgE\j\:` Falk Tannhäuser wrote: > Well, I tried to google what Ada "downward closures" are but since I > don't know the language, I'm not sure I properly understood how they > work (shame on me!). I believe downward closures are not at all Ada-specific. In Ada, you pass a function f as a parameter to another function g(..., f) *even* when f is declared at a place more deeply nested than the declaration of g. So f has access to objects up the nesting level, i.e. to its environment, g has not. For example, the Ada container library provides procedures "iterate", consuming a container and a procedure pointer. In abbreviated peudo-notation: container library: procedure iterate(a container X, a procedure P); -- invoke P for every element in X your program: procedure work (a container X) { ... local declarations procedure f (a cursor K) { -- local procedure with access to X's element a K, -- and to the local declarations ... } iterate(a container, f); } When iterate executes, both the local declarations and f still exist, they are accessible. But iterate is declared somewhere else, f is more nested. The accessibility rules imply that iterate can only invoke f, or pass it on to other procedures, but f cannot for example be assigned to a global variable because when work has executed, f becomes inaccessible. Here is a small and simple Ada 2005 example that I think comes fairly close to the (here named) lambda expression you have given. procedure find_gt_666 is vec: Vector; -- of integers here: Cursor := No_Element; -- end() procedure first_gt_666(x: Cursor) is begin if not Has_Element(here) and then Element(x) > 666 then here := x; end if; end first_gt_666; begin ... populate vec iterate(vec, first_gt_666'access); ... use Element(here) end find_gt_666; > Of course, it would be nicer, easier and more flexible if you could write > something like (syntax totally hypothetical!): > std::vector::iterator it = std::find_if(vec.begin(), vec.end(), > lambda{x; x>666} ); Eiffel agents might come even closer to this as they can be defined in place. Georg