Higher-order functions: Difference between revisions
Content added Content deleted
m ({{header|Lily}} Update for current version.) |
(Lingo added) |
||
Line 1,585: | Line 1,585: | ||
{{out}} |
{{out}} |
||
<pre>8</pre> |
<pre>8</pre> |
||
=={{header|Lingo}}== |
|||
Lingo doesn't support first-class functions. But global functions - i.e. either built-in functions or user-defined functions in movie scripts - can be specified and used by passing a "symbol". So for such global functions, the task can be solved. Here as example an implementation of a "map" function: |
|||
<lang lingo>-- in some movie script |
|||
---------------------------------------- |
|||
-- Runs provided function on all elements of the provided list, returns results as new list |
|||
-- @param {list} tList |
|||
-- @param {symbol} cbFunc |
|||
-- @param {object} [cbObj=_movie] |
|||
-- @return {list} |
|||
---------------------------------------- |
|||
on map (tList, cbFunc, cbObj) |
|||
if voidP(cbObj) then cbObj = _movie |
|||
res = [] |
|||
cnt = tList.count |
|||
repeat with i = 1 to cnt |
|||
res[i] = call(cbFunc, cbObj, tList[i]) |
|||
end repeat |
|||
return res |
|||
end</lang> |
|||
<lang lingo>l = [1, 2, 3] |
|||
-- passing the built-in function 'sin' (which is a method of the _movie object) as argument to map |
|||
res = map(l, #sin) |
|||
put res |
|||
-- [0.8415, 0.9093, 0.1411] |
|||
</lang> |
|||
=={{header|Logo}}== |
=={{header|Logo}}== |