September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -1,18 +1,18 @@
-- curry :: (Script|Handler) -> Script
on curry(f)
script
on lambda(a)
on |λ|(a)
script
on lambda(b)
lambda(a, b) of mReturn(f)
end lambda
on |λ|(b)
|λ|(a, b) of mReturn(f)
end |λ|
end script
end lambda
end |λ|
end script
end curry
-- TESTS
-- TESTS ----------------------------------------------------------------------
-- add :: Num -> Num -> Num
on add(a, b)
@ -31,61 +31,36 @@ curry(add)
-- Test 2.
curry(add)'s lambda(2)
curry(add)'s |λ|(2)
--> «script»
-- Test 3.
curry(add)'s lambda(2)'s lambda(3)
curry(add)'s |λ|(2)'s |λ|(3)
--> 5
-- 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}
-- 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))}
curry(add)'s |λ|(2), ¬
curry(add)'s |λ|(2)'s |λ|(3), ¬
map(curry(product)'s |λ|(7), enumFromTo(1, 10))}
--> {«script», «script», 5, {7, 14, 21, 28, 35, 42, 49, 56, 63, 70}}
-- GENERIC FUNCTIONS ----------------------------------------------------------
-- 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)
-- enumFromTo :: Int -> Int -> [Int]
on enumFromTo(m, n)
if n < m then
set d to -1
else
@ -96,4 +71,28 @@ on range(m, n)
set end of lst to i
end repeat
return lst
end range
end enumFromTo
-- 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 |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map
-- 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 |λ| : f
end script
end if
end mReturn

View file

@ -5,7 +5,7 @@ void main() {
return a + b;
}
alias add2 = curry!(add, 2);
alias add2 = partial!(add, 2);
writeln("Add 2 to 3: ", add(2, 3));
writeln("Add 2 to 3 (curried): ", add2(3));
}

View file

@ -0,0 +1,3 @@
(defn addN [n]
(fn [x]
(+ x n)))

View file

@ -0,0 +1,6 @@
=> (setv add2 (addN 2))
=> (add2 7)
9
=> ((addN 3) 4)
7

View file

@ -0,0 +1,34 @@
import java.util.function.BiFunction;
import java.util.function.Function;
public class Curry {
//Curry a method
public static <T, U, R> Function<T, Function<U, R>> curry(BiFunction<T, U, R> biFunction) {
return t -> u -> biFunction.apply(t, u);
}
public static int add(int x, int y) {
return x + y;
}
public static void curryMethod() {
BiFunction<Integer, Integer, Integer> bif = Curry::add;
Function<Integer, Function<Integer, Integer>> add = curry(bif);
Function<Integer, Integer> add5 = add.apply(5);
System.out.println(add5.apply(2));
}
//Or declare the curried function in one line
public static void curryDirectly() {
Function<Integer, Function<Integer, Integer>> add = x -> y -> x + y;
Function<Integer, Integer> add5 = add.apply(5);
System.out.println(add5.apply(2));
}
//prints 7 and 7
public static void main(String[] args) {
curryMethod();
curryDirectly();
}
}

View file

@ -0,0 +1,4 @@
function addN(n::Number)::Function
adder(x::Number) = n + x
return adder
end

View file

@ -0,0 +1,10 @@
// version 1.1.2
fun curriedAdd(x: Int) = { y: Int -> x + y }
fun main(args: Array<String>) {
val a = 2
val b = 3
val sum = curriedAdd(a)(b)
println("$a + $b = $sum")
}

View file

@ -0,0 +1,17 @@
sequence curries = {}
function create_curried(integer rid, sequence partial_args)
curries = append(curries,{rid,partial_args})
return length(curries) -- (return an integer id)
end function
function call_curried(integer id, sequence args)
{integer rid, sequence partial_args} = curries[id]
return call_func(rid,partial_args&args)
end function
function add(atom a, b)
return a+b
end function
integer curried = create_curried(routine_id("add"),{2})
printf(1,"2+5=%d\n",call_curried(curried,{5}))

View file

@ -1,6 +1,6 @@
#![feature(box_syntax)]
fn add_n(n : i32) -> Box<Fn(i32) -> i32 + 'static> {
box move |&: x| n + x
#![feature(conservative_impl_trait)]
fn add_n(n : i32) -> impl Fn(i32) -> i32 {
move |x| n + x
}
fn main() {

View file

@ -0,0 +1,6 @@
addOne:= Op("+").fp(1); addOne(5) //-->6
minusOne:=Op("-").fp1(1); minusOne(5) //-->4, note that this fixed 1 as the second parameter
// fix first and third parameters:
foo:=String.fpM("101","<foo>","</foo>"); foo("zkl"); //-->"<foo>zkl</foo>"
fcn g(x){x+1} f:=fcn(f,x){f(x)+x}.fp(g); f(5); //-->11
f:=fcn(f,x){f(x)+x}.fp(fcn(x){x+1}); // above with lambdas all the way down