Apply a callback to an array: Difference between revisions
m
→{{header|AppleScript}}: Adjusted text, updated primitives
m (→{{header|AppleScript}}: Adjusted text, updated primitives) |
|||
Line 173:
For a more general implementation of '''map(
<lang applescript>on run
Line 181:
{map(square, xs), ¬
filter(isEven, xs), ¬
--> {{1, 4, 9, 16, 25, 36, 49, 64, 81, 100}, {2, 4, 6, 8, 10}, 55}
end run
-- square :: Num -> Num -> Num
on square(x)
x * x
end square
-- sum :: Num -> Num -> Num
on sum(a, b)
a + b
end sum
-- isEven :: Int -> Bool
on isEven(n)
n mod 2 = 0
end isEven
-- GENERIC HIGHER ORDER FINCTIONS
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to
end repeat
return lst
end map
--
on
set v to startValue
set
repeat with i from 1 to lng▼
▲ repeat with i from 1 to lng
end repeat
▲ set v to mf's lambda(v, item i of xs, i, xs)
end
-- filter :: (a -> Bool) -> [a] -> [a]
on filter(f, xs)
set lst to {}
set
repeat with i from 1 to lng
▲ end repeat
end filter
-- Lift 2nd class handler function
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then
▲ else
property lambda : f▼
▲ property lambda : f
end mReturn▼
end script
end if
▲end mReturn</lang>
{{Out}}
|