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

@ -0,0 +1,62 @@
-- PARTIAL APPLICATION --------------------------------------------
on f1(x)
x * 2
end f1
on f2(x)
x * x
end f2
on run
tell curry(map)
set fsf1 to |λ|(f1)
set fsf2 to |λ|(f2)
end tell
{fsf1's |λ|({0, 1, 2, 3}), ¬
fsf2's |λ|({0, 1, 2, 3}), ¬
fsf1's |λ|({2, 4, 6, 8}), ¬
fsf2's |λ|({2, 4, 6, 8})}
end run
-- GENERIC FUNCTIONS --------------------------------------------
-- curry :: (Script|Handler) -> Script
on curry(f)
script
on |λ|(a)
script
on |λ|(b)
|λ|(a, b) of mReturn(f)
end |λ|
end script
end |λ|
end script
end curry
-- 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

@ -1,27 +1,11 @@
( ( fs
=
. '$!arg:?arg
&
' ( first r
. :?r
& whl
' ( !arg:%?first ?arg
& !r ($arg)$!first:?r
)
& !r
)
)
& ( partial
=
. !arg:(?f.?g)
& /('(x./('(y.($x)$($y)))$!g))$!f
)
& (f1=.2*!arg)
& (f2=.!arg^2)
& partial$(fs.f1):(=?fsf1)
& partial$(fs.f2):(=?fsf2)
& out$(fsf1$(0 1 2 3))
& out$(fsf2$(0 1 2 3))
& out$(fsf1$(2 4 6 8))
& out$(fsf2$(2 4 6 8))
( (fs=/('(x./('(y.map'($x.$y))))))
& (f1=/('(x.$x*2)))
& (f2=/('(x.$x^2)))
& (partial=/('(x./('(y.($x)$($y))))))
& (!partial$!fs)$!f1:?fsf1
& (!partial$!fs)$!f2:?fsf2
& out$(!fsf1$(0 1 2 3))
& out$(!fsf2$(0 1 2 3))
& out$(!fsf1$(2 4 6 8))
& out$(!fsf2$(2 4 6 8))
);

View file

@ -11,7 +11,7 @@ struct PApply
F f;
Arg arg;
tempalte< class F_, class Arg_ >
template< class F_, class Arg_ >
PApply( F_&& f, Arg_&& arg )
: f(std::forward<F_>(f)), arg(std::forward<Arg_>(arg))
{

View file

@ -0,0 +1,18 @@
import system'collections.
import system'routines.
import extensions.
program =
[
var partial := (:afs:af)((:s)(afs eval(af, s))).
var fs := (:f:s)(s selectBy(:x)(f eval:x); summarize(ArrayList new); toArray).
var f1 := (:x)(x * 2).
var f2 := (:x)(x * x).
var fsf1 := partial eval(fs, f1).
var fsf2 := partial eval(fs, f2).
console printLine(fsf1 eval:(2,4,6,8)).
console printLine(fsf2 eval:(2,4,6,8)).
].

View file

@ -0,0 +1,32 @@
(() => {
'use strict';
// GENERIC FUNCTIONS ------------------------------------------------------
// curry :: ((a, b) -> c) -> a -> b -> c
const curry = f => a => b => f(a, b);
// map :: (a -> b) -> [a] -> [b]
const map = curry((f, xs) => xs.map(f));
// PARTIAL APPLICATION ----------------------------------------------------
const
f1 = x => x * 2,
f2 = x => x * x,
fs = map,
fsf1 = fs(f1),
fsf2 = fs(f2);
// TEST -------------------------------------------------------------------
return [
fsf1([0, 1, 2, 3]),
fsf2([0, 1, 2, 3]),
fsf1([2, 4, 6, 8]),
fsf2([2, 4, 6, 8])
];
})();

View file

@ -0,0 +1,37 @@
(() => {
'use strict';
// GENERIC FUNCTIONS ------------------------------------------------------
// 2 or more arguments
// curry :: Function -> Function
const curry = (f, ...args) => {
const go = xs => xs.length >= f.length ? (f.apply(null, xs)) :
function () {
return go(xs.concat(Array.from(arguments)));
};
return go([].slice.call(args, 1));
};
// map :: (a -> b) -> [a] -> [b]
const map = curry((f, xs) => xs.map(f));
// PARTIAL APPLICATION ----------------------------------------------------
const
f1 = x => x * 2,
f2 = x => x * x,
fs = map,
fsf1 = fs(f1),
fsf2 = fs(f2);
// TEST -------------------------------------------------------------------
return [
fsf1([0, 1, 2, 3]),
fsf2([0, 1, 2, 3]),
fsf1([2, 4, 6, 8]),
fsf2([2, 4, 6, 8])
];
})();

View file

@ -0,0 +1,28 @@
// version 1.1.2
typealias Func = (Int) -> Int
typealias FuncS = (Func, List<Int>) -> List<Int>
fun fs(f: Func, seq: List<Int>) = seq.map { f(it) }
fun partial(fs: FuncS, f: Func) = { seq: List<Int> -> fs(f, seq) }
fun f1(n: Int) = 2 * n
fun f2(n: Int) = n * n
fun main(args: Array<String>) {
val fsf1 = partial(::fs, ::f1)
val fsf2 = partial(::fs, ::f2)
val seqs = listOf(
listOf(0, 1, 2, 3),
listOf(2, 4, 6, 8)
)
for (seq in seqs) {
println(fs(::f1, seq)) // normal
println(fsf1(seq)) // partial
println(fs(::f2, seq)) // normal
println(fsf2(seq)) // partial
println()
}
}

View file

@ -0,0 +1,26 @@
1) just define function as usual:
{def add {lambda {:a :b :c} {+ :a :b :c}}} -> add
2) and use it:
{add 1 2 3} -> 6
{{add 1} 2 3} -> 6
{{add 1 2} 3} -> 6
{{{add 1} 2} 3} -> 6
3) application:
{def fs {lambda {:f} map :f}}
{def f1 {lambda {:x} {* :x 2}}}
{def f2 {lambda {:x} {pow :x 2}}}
{def fsf1 {fs f1}}
{def fsf2 {fs f2}}
{{fsf1} 0 1 2 3}
{{fsf2} 0 1 2 3}
{{fsf1} 2 4 6 8}
{{fsf2} 2 4 6 8}
Output:
0 2 4 6
0 1 4 9
4 8 12 16
4 16 36 64

View file

@ -0,0 +1,24 @@
function fs(integer rid, sequence s)
for i=1 to length(s) do
s[i] = call_func(rid,{s[i]})
end for
return s
end function
function p_apply(sequence f, sequence args)
return call_func(f[1],{f[2],args})
end function
function f1(integer i)
return i+i
end function
function f2(integer i)
return i*i
end function
constant fsf1 = {routine_id("fs"),routine_id("f1")},
fsf2 = {routine_id("fs"),routine_id("f2")}
?p_apply(fsf1,{0,1,2,3})
?p_apply(fsf2,{2,4,6,8})

View file

@ -0,0 +1,4 @@
function p_apply(sequence ffsa, sequence extra_args)
object {fa,fx,set_args} = ffsa
return call_func(fa,{fx,set_args&extra_args})
end function

View file

@ -0,0 +1,4 @@
fcn fs(f,s){s.apply(f)} fcn f1(n){n*2} fcn f2(n){n*n}
var fsf1=fs.fp(f1), fsf2=fs.fp(f2);
fsf1([0..3]); //-->L(0,2,4,6)
fsf2([2..8,2]); //-->L(4,16,36,64)