Apply a callback to an array: Difference between revisions

Line 170:
 
 
For a more general implementation of '''map(list, function)''' and, '''reduce(list, function, startValue)''', and '''filter(list, predicate)''', we could write:
 
<lang applescript>-- [a] -> (a -> b) -> [b]
Line 192:
end repeat
end reduce
 
-- [a] -> (a -> Bool) -> [a]
on filter(xs, f)
set mf to mReturn(f)
set lst to {}
set lng to length of xs
repeat with i from 1 to lng
set v to item i of xs
if mf's call(v, i, xs) then
set end of lst to v
end if
end repeat
return lst
end filter
 
-- An ordinary AppleScript handler function
Line 210 ⟶ 225:
a + b
end sum
 
on isEven(n)
n mod 2 = 0
end isEven
 
 
set lstRangexs to {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
 
{map(lstRangexs, square), reduce(lstRangexs, sum, 0), filter(xs, isEven)}</lang>
 
{{Out}}
 
<pre>{{1, 4, 9, 16, 25, 36, 49, 64, 81, 100}, 55, {2, 4, 6, 8, 10}}</pre>
 
=={{header|AutoHotkey}}==
9,655

edits