Currying: Difference between revisions
Content added Content deleted
(→{{header|AppleScript}}: Updated primitives) |
|||
Line 21: | Line 21: | ||
=={{header|AppleScript}}== |
=={{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. |
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: | Line 27: | ||
on curry(f) |
on curry(f) |
||
script |
script |
||
on |
on |λ|(a) |
||
script |
script |
||
on |
on |λ|(b) |
||
|λ|(a, b) of mReturn(f) |
|||
end |
end |λ| |
||
end script |
end script |
||
end |
end |λ| |
||
end script |
end script |
||
end curry |
end curry |
||
-- TESTS ---------------------------------------------------------------------- |
|||
-- TESTS |
|||
-- add :: Num -> Num -> Num |
-- add :: Num -> Num -> Num |
||
Line 58: | Line 57: | ||
-- Test 2. |
-- Test 2. |
||
curry(add)'s |
curry(add)'s |λ|(2) |
||
--> «script» |
--> «script» |
||
Line 64: | Line 63: | ||
-- Test 3. |
-- Test 3. |
||
curry(add)'s |
curry(add)'s |λ|(2)'s |λ|(3) |
||
--> 5 |
--> 5 |
||
Line 70: | Line 69: | ||
-- Test 4. |
-- Test 4. |
||
map(curry(product)'s |
map(curry(product)'s |λ|(7), enumFromTo(1, 10)) |
||
--> {7, 14, 21, 28, 35, 42, 49, 56, 63, 70} |
--> {7, 14, 21, 28, 35, 42, 49, 56, 63, 70} |
||
Line 77: | Line 76: | ||
-- Combined: |
-- Combined: |
||
{curry(add), ¬ |
{curry(add), ¬ |
||
curry(add)'s |
curry(add)'s |λ|(2), ¬ |
||
curry(add)'s |
curry(add)'s |λ|(2)'s |λ|(3), ¬ |
||
map(curry(product)'s |
map(curry(product)'s |λ|(7), enumFromTo(1, 10))} |
||
--> {«script», «script», 5, {7, 14, 21, 28, 35, 42, 49, 56, 63, 70}} |
--> {«script», «script», 5, {7, 14, 21, 28, 35, 42, 49, 56, 63, 70}} |
||
-- GENERIC FUNCTIONS ---------------------------------------------------------- |
|||
⚫ | |||
-- GENERIC FUNCTIONS |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
else |
else |
||
set d to 1 |
|||
⚫ | |||
end script |
|||
end if |
end if |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end enumFromTo |
|||
-- map :: (a -> b) -> [a] -> [b] |
-- map :: (a -> b) -> [a] -> [b] |
||
Line 105: | Line 105: | ||
set lst to {} |
set lst to {} |
||
repeat with i from 1 to lng |
repeat with i from 1 to lng |
||
set end of lst to |
set end of lst to |λ|(item i of xs, i, xs) |
||
end repeat |
end repeat |
||
return lst |
return lst |
||
Line 111: | Line 111: | ||
end map |
end map |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
if class of f is script then |
|||
⚫ | |||
else |
else |
||
script |
|||
⚫ | |||
⚫ | |||
end if |
end if |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end range</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre>{«script», «script», 5, {7, 14, 21, 28, 35, 42, 49, 56, 63, 70}}</pre> |
<pre>{«script», «script», 5, {7, 14, 21, 28, 35, 42, 49, 56, 63, 70}}</pre> |