Currying: Difference between revisions
→{{header|AppleScript}}: Updated primitives
(→{{header|AppleScript}}: Updated primitives) |
|||
Line 21:
=={{header|AppleScript}}==
The nearest thing to a first-class function in AppleScript is a 'script' in which a 'handler' (with some default or vanilla name like 'call' or 'lambda' is embedded). First class use of an ordinary 2nd class 'handler' function requires 'lifting' it into an enclosing script – a process which can be abstracted to a general mReturn function.
Line 28 ⟶ 27:
on curry(f)
script
on
script
on
end
end script
end
end script
end curry
-- TESTS ----------------------------------------------------------------------
-- add :: Num -> Num -> Num
Line 58 ⟶ 57:
-- Test 2.
curry(add)'s
--> «script»
Line 64 ⟶ 63:
-- Test 3.
curry(add)'s
--> 5
Line 70 ⟶ 69:
-- Test 4.
map(curry(product)'s
--> {7, 14, 21, 28, 35, 42, 49, 56, 63, 70}
Line 77 ⟶ 76:
-- Combined:
{curry(add), ¬
curry(add)'s
curry(add)'s
map(curry(product)'s
--> {«script», «script», 5, {7, 14, 21, 28, 35, 42, 49, 56, 63, 70}}
-- GENERIC FUNCTIONS ----------------------------------------------------------
if n < m then▼
-- Lift 2nd class handler function into 1st class script wrapper ▼
-- mReturn :: Handler -> Script▼
on mReturn(f)▼
if class of f is script then▼
f▼
else
property lambda : f▼
end if
end mReturn▼
repeat with i from m to n by d▼
end repeat▼
return lst▼
end enumFromTo
-- map :: (a -> b) -> [a] -> [b]
Line 105:
set lst to {}
repeat with i from 1 to lng
set end of lst to
end repeat
return lst
Line 111:
end map
▲-- Lift 2nd class handler function into 1st class script wrapper
▲-- range :: Int -> Int -> [Int]
▲-- mReturn :: Handler -> Script
▲on range(m, n)
▲on mReturn(f)
▲ if n < m then
if class of f
▲ f
else
end if
▲end mReturn</lang>
▲ set lst to {}
▲ repeat with i from m to n by d
▲ set end of lst to i
▲ end repeat
▲ return lst
{{Out}}
<pre>{«script», «script», 5, {7, 14, 21, 28, 35, 42, 49, 56, 63, 70}}</pre>
|