Higher-order functions: Difference between revisions

Content added Content deleted
No edit summary
(Add CLU)
Line 822: Line 822:
(println (modify-string append-hello "World!"))
(println (modify-string append-hello "World!"))
</lang>
</lang>

=={{header|CLU}}==
<lang clu>% Functions can be passed to other functions using the 'proctype'
% type generator. The same works for iterators, using 'itertype'

% Here are two functions
square = proc (n: int) returns (int) return (n*n) end square
cube = proc (n: int) returns (int) return (n*n*n) end cube

% Here is a function that takes another function
do_calcs = proc (from, to: int, title: string,
fn: proctype (int) returns (int))
po: stream := stream$primary_output()
stream$putleft(po, title, 8)
stream$puts(po, " -> ")
for i: int in int$from_to(from,to) do
stream$putright(po, int$unparse(fn(i)), 5)
end
stream$putc(po, '\n')
end do_calcs

start_up = proc ()
do_calcs(1, 10, "Squares", square)
do_calcs(1, 10, "Cubes", cube)
end start_up</lang>
{{out}}
<pre>Squares -> 1 4 9 16 25 36 49 64 81 100
Cubes -> 1 8 27 64 125 216 343 512 729 1000</pre>


=={{header|CoffeeScript}}==
=={{header|CoffeeScript}}==