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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,15bb83df7cabf157 X-Google-Attributes: gid103376,public From: adam@irvine.com Subject: Re: is there a FAQ? Date: 1998/05/15 Message-ID: <6ji9o8$5t6$1@nnrp1.dejanews.com>#1/1 X-Deja-AN: 353598202 X-Http-User-Agent: Mozilla/3.0 (X11; I; Linux 2.0.18 i586) Organization: Deja News - The Leader in Internet Discussion X-Article-Creation-Date: Fri May 15 20:49:12 1998 GMT Newsgroups: comp.lang.ada Date: 1998-05-15T00:00:00+00:00 List-Id: Johann Hibschman wrote: > > I'm also just looking into Ada. I tried reading the FAQ, but I > couldn't find a could very basic questions answered. Does Ada support > functional programming and closures? I'm a bit of a language > hobbyist, and I've become quite addicted to easy temporary function > creation for mapping/reduce/etc uses. > > Is there a (convenient) way to write > > (map-into out-vector > #'(lambda (x y) (+ x (sqrt y))) > vector1 vector2) > > as I would in Common Lisp? (Or, with slight syntax changes, in ML?) I don't really know what "map-into" does; in my hazy recollection, I only remember something like "mapcar", which takes a lambda-function and one or more input lists and returns an output list. I assume that "map-into" does something similar. Ada doesn't have a built-in mapping routine, but it's pretty easy to create one. I'll assume that a "vector" is implemented as an array. generic type ELEMENT_TYPE is private; type INDEX is (<>); type ARRAY_TYPE is array (INDEX range <>) of ELEMENT_TYPE; with function MAP_FUNCTION (Left, Right : ELEMENT_TYPE) return ELEMENT_TYPE; procedure Generic_Map (Input_1, Input_2 : in ARRAY_TYPE; Result : out ARRAY_TYPE); procedure Generic_Map (Input_1, Input_2 : in ARRAY_TYPE; Result : out ARRAY_TYPE) is begin for I in Input_1'range loop Result (I) := MAP_FUNCTION (Input_1 (I), Input_2 (I)); end loop; end Generic_Map; I've made the assumption that Input_1, Input_2, and Result all have the same bounds. In real life, you'd probably want to check that yourself. Now, to use the Map procedure, we just have to tell it about our element type, our array type, and what mapping function we plan to use: procedure Demonstrate is type Float_Array is array (Integer range <>) of Float; Array_1, Array_2, Array_3 : Float_Array (1 .. 10); function My_Function (X, Y : Float) return Float; procedure Map_My_Function is new Generic_Map (ELEMENT_TYPE => Float, INDEX => Integer, ARRAY_TYPE => Float_Array, MAP_FUNCTION => My_Function); function My_Function (X, Y : Float) return Float is begin return X + Sqrt (Y); end My_Function; begin ...... Map_My_Function (Array_1, Array_2, Array_3); end Demonstrate; A little more verbose than Common Lisp, but not that much, really. No more than you'd expect for a strongly typed language. Hope this helps, -- Adam -----== Posted via Deja News, The Leader in Internet Discussion ==----- http://www.dejanews.com/ Now offering spam-free web-based newsreading