Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
|
|
@ -0,0 +1,50 @@
|
|||
with Ada.Text_IO;
|
||||
|
||||
procedure Partial_Function_Application is
|
||||
|
||||
type Sequence is array(Positive range <>) of Integer;
|
||||
|
||||
-- declare a function FS with a generic parameter F and a normal parameter S
|
||||
generic
|
||||
with function F(I: Integer) return Integer; -- generic parameter
|
||||
function FS (S: Sequence) return Sequence;
|
||||
|
||||
-- define FS
|
||||
function FS (S: Sequence) return Sequence is
|
||||
Result: Sequence(S'First .. S'Last);
|
||||
begin
|
||||
for Idx in S'Range loop
|
||||
Result(Idx) := F(S(Idx));
|
||||
end loop;
|
||||
return Result;
|
||||
end FS;
|
||||
|
||||
-- define functions F1 and F2
|
||||
function F1(I: Integer) return Integer is
|
||||
begin
|
||||
return 2*I;
|
||||
end F1;
|
||||
|
||||
function F2(I: Integer) return Integer is
|
||||
begin
|
||||
return I**2;
|
||||
end F2;
|
||||
|
||||
-- instantiate the function FS by F1 and F2 (partially apply F1 and F2 to FS)
|
||||
function FSF1 is new FS(F1);
|
||||
function FSF2 is new FS(F2);
|
||||
|
||||
procedure Print(S: Sequence) is
|
||||
begin
|
||||
for Idx in S'Range loop
|
||||
Ada.Text_IO.Put(Integer'Image(S(Idx)));
|
||||
end loop;
|
||||
Ada.Text_IO.New_Line;
|
||||
end Print;
|
||||
|
||||
begin
|
||||
Print(FSF1((0,1,2,3)));
|
||||
Print(FSF2((0,1,2,3)));
|
||||
Print(FSF1((2,4,6,8)));
|
||||
Print(FSF2((2,4,6,8)));
|
||||
end Partial_Function_Application;
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
fs = ->(f : Int32 -> Int32, s : Array(Int32)) { s.map {|elt| f[elt] } }
|
||||
|
||||
f1 = ->(n : Int32) { n * 2 }
|
||||
f2 = ->(n : Int32) { n * n }
|
||||
|
||||
fsf1 = fs.partial(f1)
|
||||
fsf2 = fs.partial(f2)
|
||||
|
||||
[(0..3).to_a, (2..8).step(2).to_a].each do |arg|
|
||||
[{fsf1, "fsf1"}, {fsf2, "fsf2"}].each do |fn , name|
|
||||
puts "#{name} #{arg} = #{fn[arg]}"
|
||||
end
|
||||
puts
|
||||
end
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
(defn fs [f s] (map f s))
|
||||
(defn f1 [x] (* 2 x))
|
||||
(defn f2 [x] (* x x))
|
||||
(def fsf1 (partial fs f1))
|
||||
(def fsf2 (partial fs f2))
|
||||
|
||||
(each s [@[0 1 2 3] @[2 4 6 8]]
|
||||
(printf "Results for array %n." s)
|
||||
(printf "fsf1: %n." (fsf1 s))
|
||||
(printf "fsf2: %n." (fsf2 s)))
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
const fs = (f, s) => s.map(f);
|
||||
const f1 = x => x * 2;
|
||||
const f2 = x => x * x;
|
||||
|
||||
const fsf1 = fs.bind(null, f1);
|
||||
const fsf2 = fs.bind(null, f2);
|
||||
|
||||
console.log(fsf1([0, 1, 2, 3])); // [0, 2, 4, 6]
|
||||
console.log(fsf2([0, 1, 2, 3])); // [0, 1, 4, 9]
|
||||
|
||||
console.log(fsf1([2, 4, 6, 8])); // [4, 8, 12, 16]
|
||||
console.log(fsf2([2, 4, 6, 8])); // [4, 16, 36, 64]
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
$f1 = fn($x) => $x * 2;
|
||||
$f2 = fn($x) => $x * $x;
|
||||
|
||||
$fsf1 = array_map($f1, ?);
|
||||
$fsf2 = array_map($f2, ?);
|
||||
|
||||
var_dump($fsf1([0, 1, 2, 3])); // [0, 2, 4, 6]
|
||||
var_dump($fsf2([0, 1, 2, 3])); // [0, 1, 4, 9]
|
||||
|
||||
var_dump($fsf1([2, 4, 6, 8])); // [4, 8, 12, 16]
|
||||
var_dump($fsf2([2, 4, 6, 8])); // [4, 16, 36, 64]
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
local fmt = require "fmt"
|
||||
|
||||
local fs = |f, s| -> s:mapped(|e| -> f(e))
|
||||
local f1 = |n| -> 2 * n
|
||||
local f2 = |n| -> n * n
|
||||
|
||||
local partial = |f, g| -> (|x| -> f(g, x))
|
||||
|
||||
local ss = {{0, 1, 2, 3}, {2, 4, 6, 8}}
|
||||
for ss as s do
|
||||
local fsf1 = partial(fs, f1)
|
||||
local fsf2 = partial(fs, f2)
|
||||
fmt.lprint(fsf1(s))
|
||||
fmt.lprint(fsf2(s))
|
||||
print()
|
||||
end
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
Red[ "Partial Function Application in Red - Hinjo, 21 July 2025" ]
|
||||
|
||||
; define a custome simple currying function
|
||||
c!: func ['f x][func [y][compose [(f) (x) y]] ; update: simplified!
|
||||
|
||||
; test with Red's built-in functions "add" and "multiply"
|
||||
add10: c! add 10
|
||||
print add10 5
|
||||
print add10 7
|
||||
|
||||
double: c! multiply 2
|
||||
print double 5
|
||||
print double 100
|
||||
|
||||
; test with a custom function to get x to power of y
|
||||
my-power: func [x y][either y = 0 [1][tot: 1 loop y [tot: tot * x]]]
|
||||
|
||||
; create a binary power by currying it
|
||||
my-binary-power: c! my-power 2
|
||||
foreach i [0 1 2 3 4 5 6 7 8 9 10] [print my-binary-power i]
|
||||
|
||||
; this is currying the built-in Red's "if" function!
|
||||
doit: c! if true
|
||||
doit [print "Hello currying world!"]
|
||||
|
||||
; simulate a realistic use
|
||||
random/seed now
|
||||
test: func [bCond bAction] [if do bCond bAction]
|
||||
system-failed?: c! test [(random 100) > 80]
|
||||
|
||||
; how many time failures in ten years?
|
||||
fail: 0 year: 1 while [year <= 10] [ print [year " "]
|
||||
system-failed? [ fail: fail + 1 print "Maintenance!" ]
|
||||
year: year + 1
|
||||
]
|
||||
print ["Failed " fail]
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
fs: ?map
|
||||
f1: fn1 { * 2 }
|
||||
f2: fn { n } { * n }
|
||||
fsf1: fn1 { .fs ?f1 }
|
||||
fsf2: fn1 { .fs ?f2 }
|
||||
|
||||
print fsf1 { 0 1 2 3 }
|
||||
print fsf2 { 0 1 2 3 }
|
||||
print fsf1 { 2 4 6 8 }
|
||||
print fsf2 { 2 4 6 8 }
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
struct PartialFs {
|
||||
f Func @[required]
|
||||
fs FuncS @[required]
|
||||
}
|
||||
|
||||
type Func = fn (int) int
|
||||
type FuncS = fn (Func, []int) []int
|
||||
|
||||
fn fs(f Func, seq []int) []int { return seq.map(f) }
|
||||
|
||||
fn (p PartialFs) call(seq []int) []int { return p.fs(p.f, seq) }
|
||||
|
||||
fn f1(n int) int { return 2 * n }
|
||||
|
||||
fn f2(n int) int { return n * n }
|
||||
|
||||
fn main() {
|
||||
fsf1 := PartialFs{f: f1, fs: fs}
|
||||
fsf2 := PartialFs{f: f2, fs: fs}
|
||||
|
||||
seqs := [
|
||||
[0, 1, 2, 3],
|
||||
[2, 4, 6, 8],
|
||||
]
|
||||
|
||||
for seq in seqs {
|
||||
println(fs(f1, seq))
|
||||
println(fsf1.call(seq))
|
||||
println(fs(f2, seq))
|
||||
println(fsf2.call(seq))
|
||||
println("")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue