2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -3,3 +3,4 @@ Create a simple demonstrative example of [[wp:Currying|Currying]] in the specifi
|
|||
|
||||
Add any historic details as to how the feature made its way into the language.
|
||||
<!-- from: http://en.wikipedia.org/w/index.php?title=Currying&direction=prev&oldid=142127294 -->
|
||||
[[Category:Functions and subroutines]]
|
||||
|
|
|
|||
99
Task/Currying/AppleScript/currying.applescript
Normal file
99
Task/Currying/AppleScript/currying.applescript
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
-- curry :: (Script|Handler) -> Script
|
||||
on curry(f)
|
||||
script
|
||||
on lambda(a)
|
||||
script
|
||||
on lambda(b)
|
||||
lambda(a, b) of mReturn(f)
|
||||
end lambda
|
||||
end script
|
||||
end lambda
|
||||
end script
|
||||
end curry
|
||||
|
||||
|
||||
-- TESTS
|
||||
|
||||
-- add :: Num -> Num -> Num
|
||||
on add(a, b)
|
||||
a + b
|
||||
end add
|
||||
|
||||
-- product :: Num -> Num -> Num
|
||||
on product(a, b)
|
||||
a * b
|
||||
end product
|
||||
|
||||
-- Test 1.
|
||||
curry(add)
|
||||
|
||||
--> «script»
|
||||
|
||||
|
||||
-- Test 2.
|
||||
curry(add)'s lambda(2)
|
||||
|
||||
--> «script»
|
||||
|
||||
|
||||
-- Test 3.
|
||||
curry(add)'s lambda(2)'s lambda(3)
|
||||
|
||||
--> 5
|
||||
|
||||
|
||||
-- Test 4.
|
||||
map(curry(product)'s lambda(7), range(1, 10))
|
||||
|
||||
--> {7, 14, 21, 28, 35, 42, 49, 56, 63, 70}
|
||||
|
||||
|
||||
-- Combined:
|
||||
{curry(add), ¬
|
||||
curry(add)'s lambda(2), ¬
|
||||
curry(add)'s lambda(2)'s lambda(3), ¬
|
||||
map(curry(product)'s lambda(7), range(1, 10))}
|
||||
|
||||
--> {«script», «script», 5, {7, 14, 21, 28, 35, 42, 49, 56, 63, 70}}
|
||||
|
||||
|
||||
|
||||
-- GENERIC FUNCTIONS
|
||||
|
||||
-- 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
|
||||
script
|
||||
property lambda : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
||||
-- map :: (a -> b) -> [a] -> [b]
|
||||
on map(f, xs)
|
||||
tell mReturn(f)
|
||||
set lng to length of xs
|
||||
set lst to {}
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to lambda(item i of xs, i, xs)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end map
|
||||
|
||||
-- range :: Int -> Int -> [Int]
|
||||
on range(m, n)
|
||||
if n < m then
|
||||
set d to -1
|
||||
else
|
||||
set d to 1
|
||||
end if
|
||||
set lst to {}
|
||||
repeat with i from m to n by d
|
||||
set end of lst to i
|
||||
end repeat
|
||||
return lst
|
||||
end range
|
||||
1
Task/Currying/JavaScript/currying-10.js
Normal file
1
Task/Currying/JavaScript/currying-10.js
Normal file
|
|
@ -0,0 +1 @@
|
|||
[7, 14, 21, 28, 35, 42, 49, 56, 63, 70]
|
||||
29
Task/Currying/JavaScript/currying-11.js
Normal file
29
Task/Currying/JavaScript/currying-11.js
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
(() => {
|
||||
|
||||
// (arbitrary arity to fully curried)
|
||||
// extraCurry :: Function -> Function
|
||||
let extraCurry = (f, ...args) => {
|
||||
let intArgs = f.length;
|
||||
|
||||
// Recursive currying
|
||||
let _curry = (xs, ...arguments) =>
|
||||
xs.length >= intArgs ? (
|
||||
f.apply(null, xs)
|
||||
) : function () {
|
||||
return _curry(xs.concat([].slice.apply(arguments)));
|
||||
};
|
||||
|
||||
return _curry([].slice.call(args, 1));
|
||||
};
|
||||
|
||||
// TEST
|
||||
|
||||
// product3:: Num -> Num -> Num -> Num
|
||||
let product3 = (a, b, c) => a * b * c;
|
||||
|
||||
return [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
.map(extraCurry(product3)(7)(2))
|
||||
|
||||
// [14, 28, 42, 56, 70, 84, 98, 112, 126, 140]
|
||||
|
||||
})();
|
||||
1
Task/Currying/JavaScript/currying-12.js
Normal file
1
Task/Currying/JavaScript/currying-12.js
Normal file
|
|
@ -0,0 +1 @@
|
|||
[14, 28, 42, 56, 70, 84, 98, 112, 126, 140]
|
||||
|
|
@ -1 +1,34 @@
|
|||
(a,b) => expr_using_a_and_b
|
||||
(function () {
|
||||
|
||||
// curry :: ((a, b) -> c) -> a -> b -> c
|
||||
function curry(f) {
|
||||
return function (a) {
|
||||
return function (b) {
|
||||
return f(a, b);
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
// TESTS
|
||||
|
||||
// product :: Num -> Num -> Num
|
||||
function product(a, b) {
|
||||
return a * b;
|
||||
}
|
||||
|
||||
// return typeof curry(product);
|
||||
// --> function
|
||||
|
||||
// return typeof curry(product)(7)
|
||||
// --> function
|
||||
|
||||
//return typeof curry(product)(7)(9)
|
||||
// --> number
|
||||
|
||||
return [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
.map(curry(product)(7))
|
||||
|
||||
// [7, 14, 21, 28, 35, 42, 49, 56, 63, 70]
|
||||
|
||||
})();
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
a => b => expr_using_a_and_b
|
||||
[7, 14, 21, 28, 35, 42, 49, 56, 63, 70]
|
||||
|
|
|
|||
|
|
@ -1,23 +1,34 @@
|
|||
let
|
||||
fix = // This is a variant of the Applicative order Y combinator
|
||||
f => (f => f(f))(g => f((...a) => g(g)(...a))),
|
||||
curry =
|
||||
f => (
|
||||
fix(
|
||||
z => (n,...a) => (
|
||||
n>0
|
||||
?b => z(n-1,...a,b)
|
||||
:f(...a)))
|
||||
(f.length)),
|
||||
curryrest =
|
||||
f => (
|
||||
fix(
|
||||
z => (n,...a) => (
|
||||
n>0
|
||||
?b => z(n-1,...a,b)
|
||||
:(...b) => f(...a,...b)))
|
||||
(f.length)),
|
||||
curriedmax=curry(Math.max),
|
||||
curryrestedmax=curryrest(Math.max);
|
||||
print(curriedmax(8)(4),curryrestedmax(8)(4)(),curryrestedmax(8)(4)(9,7,2));
|
||||
// 8,8,9
|
||||
(function () {
|
||||
|
||||
// (arbitrary arity to fully curried)
|
||||
// extraCurry :: Function -> Function
|
||||
function extraCurry(f) {
|
||||
|
||||
// Recursive currying
|
||||
function _curry(xs) {
|
||||
return xs.length >= intArgs ? (
|
||||
f.apply(null, xs)
|
||||
) : function () {
|
||||
return _curry(xs.concat([].slice.apply(arguments)));
|
||||
};
|
||||
}
|
||||
|
||||
var intArgs = f.length;
|
||||
|
||||
return _curry([].slice.call(arguments, 1));
|
||||
}
|
||||
|
||||
|
||||
// TEST
|
||||
|
||||
// product3:: Num -> Num -> Num -> Num
|
||||
function product3(a, b, c) {
|
||||
return a * b * c;
|
||||
}
|
||||
|
||||
return [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
.map(extraCurry(product3)(7)(2))
|
||||
|
||||
// [14, 28, 42, 56, 70, 84, 98, 112, 126, 140]
|
||||
|
||||
})();
|
||||
|
|
|
|||
1
Task/Currying/JavaScript/currying-5.js
Normal file
1
Task/Currying/JavaScript/currying-5.js
Normal file
|
|
@ -0,0 +1 @@
|
|||
[14, 28, 42, 56, 70, 84, 98, 112, 126, 140]
|
||||
1
Task/Currying/JavaScript/currying-6.js
Normal file
1
Task/Currying/JavaScript/currying-6.js
Normal file
|
|
@ -0,0 +1 @@
|
|||
(a,b) => expr_using_a_and_b
|
||||
1
Task/Currying/JavaScript/currying-7.js
Normal file
1
Task/Currying/JavaScript/currying-7.js
Normal file
|
|
@ -0,0 +1 @@
|
|||
a => b => expr_using_a_and_b
|
||||
23
Task/Currying/JavaScript/currying-8.js
Normal file
23
Task/Currying/JavaScript/currying-8.js
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
let
|
||||
fix = // This is a variant of the Applicative order Y combinator
|
||||
f => (f => f(f))(g => f((...a) => g(g)(...a))),
|
||||
curry =
|
||||
f => (
|
||||
fix(
|
||||
z => (n,...a) => (
|
||||
n>0
|
||||
?b => z(n-1,...a,b)
|
||||
:f(...a)))
|
||||
(f.length)),
|
||||
curryrest =
|
||||
f => (
|
||||
fix(
|
||||
z => (n,...a) => (
|
||||
n>0
|
||||
?b => z(n-1,...a,b)
|
||||
:(...b) => f(...a,...b)))
|
||||
(f.length)),
|
||||
curriedmax=curry(Math.max),
|
||||
curryrestedmax=curryrest(Math.max);
|
||||
print(curriedmax(8)(4),curryrestedmax(8)(4)(),curryrestedmax(8)(4)(9,7,2));
|
||||
// 8,8,9
|
||||
27
Task/Currying/JavaScript/currying-9.js
Normal file
27
Task/Currying/JavaScript/currying-9.js
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
(() => {
|
||||
|
||||
// curry :: ((a, b) -> c) -> a -> b -> c
|
||||
let curry = f => a => b => f(a, b);
|
||||
|
||||
|
||||
// TEST
|
||||
|
||||
// product :: Num -> Num -> Num
|
||||
let product = (a, b) => a * b,
|
||||
|
||||
// Int -> Int -> Maybe Int -> [Int]
|
||||
range = (m, n, step) => {
|
||||
let d = (step || 1) * (n >= m ? 1 : -1);
|
||||
|
||||
return Array.from({
|
||||
length: Math.floor((n - m) / d) + 1
|
||||
}, (_, i) => m + (i * d));
|
||||
}
|
||||
|
||||
|
||||
return range(1, 10)
|
||||
.map(curry(product)(7))
|
||||
|
||||
// [7, 14, 21, 28, 35, 42, 49, 56, 63, 70]
|
||||
|
||||
})();
|
||||
17
Task/Currying/Lua/currying.lua
Normal file
17
Task/Currying/Lua/currying.lua
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
function curry2(f)
|
||||
return function(x)
|
||||
return function(y)
|
||||
return f(x,y)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function add(x,y)
|
||||
return x+y
|
||||
end
|
||||
|
||||
local adder = curry2(add)
|
||||
assert(adder(3)(4) == 3+4)
|
||||
local add2 = adder(2)
|
||||
assert(add2(3) == 2+3)
|
||||
assert(add2(5) == 2+5)
|
||||
1
Task/Currying/PowerShell/currying-1.psh
Normal file
1
Task/Currying/PowerShell/currying-1.psh
Normal file
|
|
@ -0,0 +1 @@
|
|||
function Add($x) { return { param($y) return $y + $x }.GetNewClosure() }
|
||||
1
Task/Currying/PowerShell/currying-2.psh
Normal file
1
Task/Currying/PowerShell/currying-2.psh
Normal file
|
|
@ -0,0 +1 @@
|
|||
& (Add 1) 2
|
||||
1
Task/Currying/PowerShell/currying-3.psh
Normal file
1
Task/Currying/PowerShell/currying-3.psh
Normal file
|
|
@ -0,0 +1 @@
|
|||
(4,9,16,25 | ForEach-Object { & (add $_) ([Math]::Sqrt($_)) }) -join ", "
|
||||
1
Task/Currying/TXR/currying.txr
Normal file
1
Task/Currying/TXR/currying.txr
Normal file
|
|
@ -0,0 +1 @@
|
|||
(op - 10 @1 @2 5)
|
||||
Loading…
Add table
Add a link
Reference in a new issue