Partial function application: Difference between revisions
Content deleted Content added
Updated D version |
→{{header|Ada}}: Added Ada version |
||
Line 27: | Line 27: | ||
* In partially applying the functions f1 or f2 to fs, there should be no ''explicit'' mention of any other parameters to fs, although introspection of fs within the partial applicator to find its parameters ''is'' allowed. |
* In partially applying the functions f1 or f2 to fs, there should be no ''explicit'' mention of any other parameters to fs, although introspection of fs within the partial applicator to find its parameters ''is'' allowed. |
||
* This task is more about ''how'' results are generated rather than just getting results. |
* This task is more about ''how'' results are generated rather than just getting results. |
||
=={{header|Ada}}== |
|||
<lang Ada>with Ada.Text_IO; |
|||
procedure Partial_Function_Application is |
|||
type Sequence is array(Positive range <>) of Integer; |
|||
-- declare a function FS with a generic parameter F and a normal parameter S |
|||
generic |
|||
with function F(I: Integer) return Integer; -- generic parameter |
|||
function FS (S: Sequence) return Sequence; |
|||
-- define FS |
|||
function FS (S: Sequence) return Sequence is |
|||
Result: Sequence(S'First .. S'Last); |
|||
begin |
|||
for Idx in S'Range loop |
|||
Result(Idx) := F(S(Idx)); |
|||
end loop; |
|||
return Result; |
|||
end FS; |
|||
-- define functions F1 and F2 |
|||
function F1(I: Integer) return Integer is |
|||
begin |
|||
return 2*I; |
|||
end F1; |
|||
function F2(I: Integer) return Integer is |
|||
begin |
|||
return I**2; |
|||
end F2; |
|||
-- instantiate the function FS by F1 and F2 (partially apply F1 and F2 to FS) |
|||
function FSF1 is new FS(F1); |
|||
function FSF2 is new FS(F2); |
|||
procedure Print(S: Sequence) is |
|||
begin |
|||
for Idx in S'Range loop |
|||
Ada.Text_IO.Put(Integer'Image(S(Idx))); |
|||
end loop; |
|||
Ada.Text_IO.New_Line; |
|||
end Print; |
|||
begin |
|||
Print(FSF1((0,1,2,3))); |
|||
Print(FSF2((0,1,2,3))); |
|||
Print(FSF1((2,4,6,8))); |
|||
Print(FSF2((2,4,6,8))); |
|||
end Partial_Function_Application;</lang> |
|||
Output: |
|||
<pre> 0 2 4 6 |
|||
0 1 4 9 |
|||
4 8 12 16 |
|||
4 16 36 64</pre> |
|||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |