comp.lang.ada
 help / color / mirror / Atom feed
From: adam@irvine.com
Subject: Re: is there a FAQ?
Date: 1998/05/15
Date: 1998-05-15T00:00:00+00:00	[thread overview]
Message-ID: <6ji9o8$5t6$1@nnrp1.dejanews.com> (raw)


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




             reply	other threads:[~1998-05-15  0:00 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-05-15  0:00 adam [this message]
1998-05-15  0:00 ` is there a FAQ? Brian Rogoff
     [not found]   ` <Et0xrx.798@world.std.com>
1998-05-16  0:00     ` Fergus Henderson
1998-05-16  0:00     ` Brian Rogoff
  -- strict thread matches above, loose matches on Subject: below --
1998-05-13  0:00 Cristian Libardo
1998-05-13  0:00 ` Markus Kuhn
1998-05-15  0:00   ` Johann Hibschman
1998-05-15  0:00     ` Markus Kuhn
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox