Data update
This commit is contained in:
parent
5150844a7d
commit
4bb20c9b71
7735 changed files with 38060 additions and 199180 deletions
|
|
@ -1,30 +0,0 @@
|
|||
generic
|
||||
type Argument_1 (<>) is limited private;
|
||||
type Argument_2 (<>) is limited private;
|
||||
type Argument_3 (<>) is limited private;
|
||||
type Return_Value (<>) is limited private;
|
||||
|
||||
with function Func
|
||||
(A : in Argument_1;
|
||||
B : in Argument_2;
|
||||
C : in Argument_3)
|
||||
return Return_Value;
|
||||
package Curry_3 is
|
||||
|
||||
generic
|
||||
First : in Argument_1;
|
||||
package Apply_1 is
|
||||
|
||||
generic
|
||||
Second : in Argument_2;
|
||||
package Apply_2 is
|
||||
|
||||
function Apply_3
|
||||
(Third : in Argument_3)
|
||||
return Return_Value;
|
||||
|
||||
end Apply_2;
|
||||
|
||||
end Apply_1;
|
||||
|
||||
end Curry_3;
|
||||
|
|
@ -1,18 +0,0 @@
|
|||
package body Curry_3 is
|
||||
|
||||
package body Apply_1 is
|
||||
|
||||
package body Apply_2 is
|
||||
|
||||
function Apply_3
|
||||
(Third : in Argument_3)
|
||||
return Return_Value is
|
||||
begin
|
||||
return Func (First, Second, Third);
|
||||
end Apply_3;
|
||||
|
||||
end Apply_2;
|
||||
|
||||
end Apply_1;
|
||||
|
||||
end Curry_3;
|
||||
|
|
@ -1,27 +0,0 @@
|
|||
with Curry_3, Ada.Text_IO;
|
||||
|
||||
procedure Curry_Test is
|
||||
|
||||
function Sum
|
||||
(X, Y, Z : in Integer)
|
||||
return Integer is
|
||||
begin
|
||||
return X + Y + Z;
|
||||
end Sum;
|
||||
|
||||
package Curried is new Curry_3
|
||||
(Argument_1 => Integer,
|
||||
Argument_2 => Integer,
|
||||
Argument_3 => Integer,
|
||||
Return_Value => Integer,
|
||||
Func => Sum);
|
||||
|
||||
package Sum_5 is new Curried.Apply_1 (5);
|
||||
package Sum_5_7 is new Sum_5.Apply_2 (7);
|
||||
Result : Integer := Sum_5_7.Apply_3 (3);
|
||||
|
||||
begin
|
||||
|
||||
Ada.Text_IO.Put_Line ("Five plus seven plus three is" & Integer'Image (Result));
|
||||
|
||||
end Curry_Test;
|
||||
|
|
@ -2,30 +2,30 @@
|
|||
#include<stdio.h>
|
||||
|
||||
long int factorial(int n){
|
||||
if(n>1)
|
||||
return n*factorial(n-1);
|
||||
return 1;
|
||||
if(n>1)
|
||||
return n*factorial(n-1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
long int sumOfFactorials(int num,...){
|
||||
va_list vaList;
|
||||
long int sum = 0;
|
||||
|
||||
va_start(vaList,num);
|
||||
|
||||
while(num--)
|
||||
sum += factorial(va_arg(vaList,int));
|
||||
|
||||
va_end(vaList);
|
||||
|
||||
return sum;
|
||||
va_list vaList;
|
||||
long int sum = 0;
|
||||
|
||||
va_start(vaList,num);
|
||||
|
||||
while(num--)
|
||||
sum += factorial(va_arg(vaList,int));
|
||||
|
||||
va_end(vaList);
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("\nSum of factorials of [1,5] : %ld",sumOfFactorials(5,1,2,3,4,5));
|
||||
printf("\nSum of factorials of [3,5] : %ld",sumOfFactorials(3,3,4,5));
|
||||
printf("\nSum of factorials of [1,3] : %ld",sumOfFactorials(3,1,2,3));
|
||||
|
||||
return 0;
|
||||
printf("\nSum of factorials of [1,5] : %ld",sumOfFactorials(5,1,2,3,4,5));
|
||||
printf("\nSum of factorials of [3,5] : %ld",sumOfFactorials(3,3,4,5));
|
||||
printf("\nSum of factorials of [1,3] : %ld",sumOfFactorials(3,1,2,3));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,25 +13,25 @@ curry_second(F,Y) ->
|
|||
% Usual curry
|
||||
|
||||
curry(Fun,Arg) ->
|
||||
case erlang:fun_info(Fun,arity) of
|
||||
{arity,0} ->
|
||||
erlang:error(badarg);
|
||||
{arity,ArityFun} ->
|
||||
create_ano_fun(ArityFun,Fun,Arg);
|
||||
_ ->
|
||||
erlang:error(badarg)
|
||||
end.
|
||||
case erlang:fun_info(Fun,arity) of
|
||||
{arity,0} ->
|
||||
erlang:error(badarg);
|
||||
{arity,ArityFun} ->
|
||||
create_ano_fun(ArityFun,Fun,Arg);
|
||||
_ ->
|
||||
erlang:error(badarg)
|
||||
end.
|
||||
|
||||
create_ano_fun(Arity,Fun,Arg) ->
|
||||
Pars =
|
||||
[{var,1,list_to_atom(lists:flatten(io_lib:format("X~p", [N])))}
|
||||
|| N <- lists:seq(2,Arity)],
|
||||
Ano =
|
||||
{'fun',1,
|
||||
{clauses,[{clause,1,Pars,[],
|
||||
[{call,1,{var,1,'Fun'},[{var,1,'Arg'}] ++ Pars}]}]}},
|
||||
{_,Result,_} = erl_eval:expr(Ano, [{'Arg',Arg},{'Fun',Fun}]),
|
||||
Result.
|
||||
Pars =
|
||||
[{var,1,list_to_atom(lists:flatten(io_lib:format("X~p", [N])))}
|
||||
|| N <- lists:seq(2,Arity)],
|
||||
Ano =
|
||||
{'fun',1,
|
||||
{clauses,[{clause,1,Pars,[],
|
||||
[{call,1,{var,1,'Fun'},[{var,1,'Arg'}] ++ Pars}]}]}},
|
||||
{_,Result,_} = erl_eval:expr(Ano, [{'Arg',Arg},{'Fun',Fun}]),
|
||||
Result.
|
||||
|
||||
% Generalization of the currying
|
||||
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
extends Node
|
||||
|
||||
func addN(n: int) -> Callable:
|
||||
return func(x):
|
||||
return n + x
|
||||
return func(x):
|
||||
return n + x
|
||||
|
||||
func _ready():
|
||||
# Test currying
|
||||
var add2 := addN(2)
|
||||
print(add2.call(7))
|
||||
# Test currying
|
||||
var add2 := addN(2)
|
||||
print(add2.call(7))
|
||||
|
||||
get_tree().quit() # Exit
|
||||
get_tree().quit() # Exit
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
curry := method(fn,
|
||||
a := call evalArgs slice(1)
|
||||
block(
|
||||
b := a clone appendSeq(call evalArgs)
|
||||
performWithArgList("fn", b)
|
||||
)
|
||||
a := call evalArgs slice(1)
|
||||
block(
|
||||
b := a clone appendSeq(call evalArgs)
|
||||
performWithArgList("fn", b)
|
||||
)
|
||||
)
|
||||
|
||||
// example:
|
||||
|
|
|
|||
|
|
@ -2,33 +2,33 @@ 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();
|
||||
}
|
||||
//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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ Module LikeGroovy {
|
|||
partsof120=Curry(divide, 120)
|
||||
Print "half of 120 is ";partsof120(2)
|
||||
Print "a third is ";partsof120(3)
|
||||
Print "and a quarter is ";partsof120(4)
|
||||
Print "and a quarter is ";partsof120(4)
|
||||
|
||||
joinTwoWordsWithSymbol=lambda (s, a, b)->a+s+b
|
||||
Assert joinTwoWordsWithSymbol("#","Hello", "World")="Hello#World"
|
||||
|
|
@ -16,44 +16,44 @@ Module LikeGroovy {
|
|||
}
|
||||
LikeGroovy
|
||||
Module M2000way {
|
||||
class curry{
|
||||
private:
|
||||
p=stack ' stack of values
|
||||
func$ ' field for the weak reference
|
||||
public:
|
||||
property counter {value } ' readonly
|
||||
class:
|
||||
module curry(.func$) {
|
||||
.p<=[]
|
||||
class value {
|
||||
value () {
|
||||
' symbol ! used for feeding stack of values from arrays or stacks.
|
||||
' [] is the current stack (leave emtpy stack as current stack)
|
||||
' Stack(.p) make a copy of .p (which have a stack object)
|
||||
=function(.func$, !stack(.p), ![])
|
||||
.[counter]++
|
||||
}
|
||||
}
|
||||
this=value() ' make this as a property
|
||||
}
|
||||
}
|
||||
' using a general function
|
||||
Function Divide(a, b) {
|
||||
=a/b
|
||||
}
|
||||
Print "Divide(10, 6) is ";Divide(10, 5)
|
||||
partsof120=Curry(&Divide(), 120)
|
||||
Print "half of 120 is ";partsof120(2)
|
||||
Print "a third is ";partsof120(3)
|
||||
Print "and a quarter is ";partsof120(4)
|
||||
Print "Use of partsof120 so far: "; partsof120.counter; " times"
|
||||
' using a lambda function
|
||||
joinTwoWordsWithSymbol=lambda (s, a, b)->a+s+b
|
||||
Assert joinTwoWordsWithSymbol("#","Hello", "World")="Hello#World"
|
||||
concatWords =Curry(&joinTwoWordsWithSymbol, " ")
|
||||
Assert concatWords("Hello", "World")="Hello World"
|
||||
prependHello =Curry(&concatWords, "Hello")
|
||||
Assert prependHello("World")="Hello World"
|
||||
Print "done"
|
||||
class curry{
|
||||
private:
|
||||
p=stack ' stack of values
|
||||
func$ ' field for the weak reference
|
||||
public:
|
||||
property counter {value } ' readonly
|
||||
class:
|
||||
module curry(.func$) {
|
||||
.p<=[]
|
||||
class value {
|
||||
value () {
|
||||
' symbol ! used for feeding stack of values from arrays or stacks.
|
||||
' [] is the current stack (leave emtpy stack as current stack)
|
||||
' Stack(.p) make a copy of .p (which have a stack object)
|
||||
=function(.func$, !stack(.p), ![])
|
||||
.[counter]++
|
||||
}
|
||||
}
|
||||
this=value() ' make this as a property
|
||||
}
|
||||
}
|
||||
' using a general function
|
||||
Function Divide(a, b) {
|
||||
=a/b
|
||||
}
|
||||
Print "Divide(10, 6) is ";Divide(10, 5)
|
||||
partsof120=Curry(&Divide(), 120)
|
||||
Print "half of 120 is ";partsof120(2)
|
||||
Print "a third is ";partsof120(3)
|
||||
Print "and a quarter is ";partsof120(4)
|
||||
Print "Use of partsof120 so far: "; partsof120.counter; " times"
|
||||
' using a lambda function
|
||||
joinTwoWordsWithSymbol=lambda (s, a, b)->a+s+b
|
||||
Assert joinTwoWordsWithSymbol("#","Hello", "World")="Hello#World"
|
||||
concatWords =Curry(&joinTwoWordsWithSymbol, " ")
|
||||
Assert concatWords("Hello", "World")="Hello World"
|
||||
prependHello =Curry(&concatWords, "Hello")
|
||||
Assert prependHello("World")="Hello World"
|
||||
Print "done"
|
||||
}
|
||||
M2000way
|
||||
|
|
|
|||
|
|
@ -11,10 +11,10 @@ module Curry
|
|||
Main() : void
|
||||
{
|
||||
def f(x, y) { x + y }
|
||||
def g = Curry(f);
|
||||
def h = Curry(f)(12); // partial application
|
||||
WriteLine($"$(Curry(f)(20)(22))");
|
||||
WriteLine($"$(g(21)(21))");
|
||||
WriteLine($"$(h(30))")
|
||||
def g = Curry(f);
|
||||
def h = Curry(f)(12); // partial application
|
||||
WriteLine($"$(Curry(f)(20)(22))");
|
||||
WriteLine($"$(g(21)(21))");
|
||||
WriteLine($"$(h(30))")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1 +0,0 @@
|
|||
function Add($x) { return { param($y) return $y + $x }.GetNewClosure() }
|
||||
|
|
@ -1 +0,0 @@
|
|||
& (Add 1) 2
|
||||
|
|
@ -1 +0,0 @@
|
|||
(4,9,16,25 | ForEach-Object { & (add $_) ([Math]::Sqrt($_)) }) -join ", "
|
||||
|
|
@ -1,7 +1,13 @@
|
|||
/*REXX program demonstrates a REXX currying method to perform addition. */
|
||||
say 'add 2 to 3: ' add(2, 3)
|
||||
say 'add 2 to 3 (curried):' add2(3)
|
||||
say 'add 2 to 3: ' add(2,3)
|
||||
say 'add 2 to 3 (curried):' add2(3)
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
add: procedure; $= arg(1); do j=2 to arg(); $= $ + arg(j); end; return $
|
||||
add2: procedure; return add( arg(1), 2)
|
||||
/*--------------------------------------------------------------------------------------*/
|
||||
add: procedure
|
||||
sum=arg(1)
|
||||
do j=2 to arg()
|
||||
sum=sum+arg(j)
|
||||
end
|
||||
return sum
|
||||
add2: procedure
|
||||
return add(arg(1),2)
|
||||
|
|
|
|||
|
|
@ -1,12 +1,16 @@
|
|||
/*REXX program demonstrates a REXX currying method to perform addition. */
|
||||
say 'add 2 to 3: ' add(2, 3)
|
||||
say 'add 2 to 3 (curried):' add2(3)
|
||||
say 'add 2 to 3: ' add(2, 3)
|
||||
say 'add 2 to 3 (curried):' add2(3)
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
add: procedure; $= 0; do j=1 for arg()
|
||||
do k=1 for words( arg(j) ); $= $ + word( arg(j), k)
|
||||
end /*k*/
|
||||
end /*j*/
|
||||
return $
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
add2: procedure; return add( arg(1), 2)
|
||||
/*--------------------------------------------------------------------------------------*/
|
||||
add: procedure
|
||||
sum=0
|
||||
do j=1 for arg()
|
||||
do k=1 for words(arg(j))
|
||||
sum=sum+word(arg(j),k)
|
||||
end /*k*/
|
||||
end /*j*/
|
||||
return sum
|
||||
/*--------------------------------------------------------------------------------------*/
|
||||
add2: procedure
|
||||
return add(arg(1),2)
|
||||
|
|
|
|||
|
|
@ -1,36 +1,96 @@
|
|||
Red[ "Currying in Red - Hinjo, 21 July 2025" ]
|
||||
Red [ "Real Currying in Red - Hinjolicious" ]
|
||||
|
||||
; define a custome simple currying function
|
||||
c!: func ['f x][do compose/deep [func [y] [do compose [(get f) (x) y]]]]
|
||||
|
||||
; 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
|
||||
; helper func
|
||||
get-args: function [f][
|
||||
collect [foreach e spec-of :f [
|
||||
if refinement? e [break]
|
||||
if any [word? e lit-word? e] [keep e]
|
||||
]]
|
||||
]
|
||||
|
||||
; currying
|
||||
curry: function [f v][
|
||||
f?: function? :f ; a func?
|
||||
ff: either f? [:f][get f] ; get func directly or by its name
|
||||
|
||||
sp: spec-of :ff ; raw spec
|
||||
a: get-args :ff ; cleaned args
|
||||
|
||||
; curried? get skip counter
|
||||
sc: either c?: sp/1 = "C!" [to-integer sp/2][0]
|
||||
|
||||
ca: copy a na: copy a
|
||||
|
||||
either v = [] [ ; skipping
|
||||
sc: sc + 1 ; spec unchanged
|
||||
][
|
||||
either sc > 0 [ ; skip some args
|
||||
remove skip ca sc ; removed curried arg from spec
|
||||
na/(sc + 1): v ; replace with value
|
||||
na: skip na sc a: skip a sc ; point to next args
|
||||
][ ; normal
|
||||
ca: next ca ; skip curried arg
|
||||
na/1: v ; replace with value
|
||||
]
|
||||
]
|
||||
|
||||
; add marker and counter
|
||||
f: func compose ["C!" (to-string sc) (ca)] either c? [ ; curried?
|
||||
replace copy body-of :ff a na ; chained curry
|
||||
][
|
||||
compose [(:f)(na)] ; first curry
|
||||
]
|
||||
either empty? ca [f][:f] ; return function or result
|
||||
]
|
||||
|
||||
->: make op! :curry ; curry operator
|
||||
|
||||
; This function will print a string as a heading and print any codes
|
||||
; to the screen before executing it.
|
||||
demo: function [s b] [print ["^/==" s "==^/^/" mold/only b "^/>>"] do b]
|
||||
|
||||
demo "Real Currying in Red" [
|
||||
|
||||
sum: func [a b c][a + b + c] ;
|
||||
]
|
||||
|
||||
demo "Curry one arg from 'sum', return func with two args (s1)" [
|
||||
s1: 'sum -> 1
|
||||
print s1 2 3 ; 6
|
||||
]
|
||||
|
||||
demo "Curry one arg from 's2', return func with one arg (s2)" [
|
||||
s2: 's1 -> 2
|
||||
print s2 3 ; 6
|
||||
]
|
||||
|
||||
demo "Curry one arg from 's3', return the result" [
|
||||
s3: 's2 -> 3
|
||||
print s3 ; 6
|
||||
]
|
||||
|
||||
demo "Currying all args at once, return the result" [
|
||||
print 'sum -> 1 -> 2 -> 3 ; 6
|
||||
]
|
||||
|
||||
demo "Currying native control" [
|
||||
i: 1
|
||||
'while -> [i <= 10] -> [print i i: i + 1]
|
||||
]
|
||||
|
||||
demo "Currying native control" [
|
||||
'repeat -> 'i -> 10 -> [print ["repeating" i]]
|
||||
]
|
||||
|
||||
demo "Currying anonymous function (lambda)" [
|
||||
print (func [a b][a + b]) -> 10 -> 20 ; 30
|
||||
]
|
||||
|
||||
demo "Skipping args" [
|
||||
f: 'sum -> [] -> [] -> 30
|
||||
print f 10 20
|
||||
]
|
||||
|
||||
demo "Sample generated code" [
|
||||
?? f
|
||||
]
|
||||
print ["Failed " fail]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue