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 lambda(a)
on |λ|(a)
script
script
on lambda(b)
on |λ|(b)
lambda(a, b) of mReturn(f)
|λ|(a, b) of mReturn(f)
end lambda
end |λ|
end script
end script
end lambda
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 lambda(2)
curry(add)'s |λ|(2)


--> «script»
--> «script»
Line 64: Line 63:


-- Test 3.
-- Test 3.
curry(add)'s lambda(2)'s lambda(3)
curry(add)'s |λ|(2)'s |λ|(3)


--> 5
--> 5
Line 70: Line 69:


-- Test 4.
-- Test 4.
map(curry(product)'s lambda(7), range(1, 10))
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 lambda(2), ¬
curry(add)'s |λ|(2), ¬
curry(add)'s lambda(2)'s lambda(3), ¬
curry(add)'s |λ|(2)'s |λ|(3), ¬
map(curry(product)'s lambda(7), range(1, 10))}
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 ----------------------------------------------------------


-- enumFromTo :: Int -> Int -> [Int]
-- GENERIC FUNCTIONS
on enumFromTo(m, n)

if n < m then
-- Lift 2nd class handler function into 1st class script wrapper
set d to -1
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then
f
else
else
script
set d to 1
property lambda : f
end script
end if
end if
set lst to {}
end mReturn
repeat with i from m to n by d
set end of lst to i
end repeat
return lst
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 lambda(item i of xs, i, xs)
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


-- 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
set d to -1
if class of f is script then
f
else
else
set d to 1
script
property |λ| : f
end script
end if
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
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>