September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
1
Task/Catamorphism/00META.yaml
Normal file
1
Task/Catamorphism/00META.yaml
Normal file
|
|
@ -0,0 +1 @@
|
|||
--- {}
|
||||
44
Task/Catamorphism/ABAP/catamorphism.abap
Normal file
44
Task/Catamorphism/ABAP/catamorphism.abap
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
report z_catamorphism.
|
||||
|
||||
data(numbers) = value int4_table( ( 1 ) ( 2 ) ( 3 ) ( 4 ) ( 5 ) ).
|
||||
|
||||
write: |numbers = { reduce string(
|
||||
init output = `[`
|
||||
index = 1
|
||||
for number in numbers
|
||||
next output = cond string(
|
||||
when index eq lines( numbers )
|
||||
then |{ output }, { number } ]|
|
||||
when index > 1
|
||||
then |{ output }, { number }|
|
||||
else |{ output } { number }| )
|
||||
index = index + 1 ) }|, /.
|
||||
|
||||
write: |sum(numbers) = { reduce int4(
|
||||
init result = 0
|
||||
for number in numbers
|
||||
next result = result + number ) }|, /.
|
||||
|
||||
write: |product(numbers) = { reduce int4(
|
||||
init result = 1
|
||||
for number in numbers
|
||||
next result = result * number ) }|, /.
|
||||
|
||||
data(strings) = value stringtab( ( `reduce` ) ( `in` ) ( `ABAP` ) ).
|
||||
|
||||
write: |strings = { reduce string(
|
||||
init output = `[`
|
||||
index = 1
|
||||
for string in strings
|
||||
next output = cond string(
|
||||
when index eq lines( strings )
|
||||
then |{ output }, { string } ]|
|
||||
when index > 1
|
||||
then |{ output }, { string }|
|
||||
else |{ output } { string }| )
|
||||
index = index + 1 ) }|, /.
|
||||
|
||||
write: |concatenation(strings) = { reduce string(
|
||||
init text = ``
|
||||
for string in strings
|
||||
next text = |{ text } { string }| ) }|, /.
|
||||
5
Task/Catamorphism/Aime/catamorphism.aime
Normal file
5
Task/Catamorphism/Aime/catamorphism.aime
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
integer s;
|
||||
|
||||
s = 0;
|
||||
list(1, 2, 3, 4, 5, 6, 7, 8, 9).ucall(add_i, 1, s);
|
||||
o_(s, "\n");
|
||||
|
|
@ -1,17 +1,17 @@
|
|||
import system'collections.
|
||||
import system'routines.
|
||||
import extensions.
|
||||
import extensions'text.
|
||||
import system'collections;
|
||||
import system'routines;
|
||||
import extensions;
|
||||
import extensions'text;
|
||||
|
||||
public program =
|
||||
[
|
||||
var numbers := 1 to:10 repeat(:n)( n ); summarize(ArrayList new).
|
||||
public program()
|
||||
{
|
||||
var numbers := new Range(1,10).summarize(new ArrayList());
|
||||
|
||||
var summary := numbers accumulate(Variable new:0) with(:a:b)( a + b ).
|
||||
var summary := numbers.accumulate(new Variable(0), (a,b => a + b));
|
||||
|
||||
var product := numbers accumulate(Variable new:1) with(:a:b)( a * b ).
|
||||
var product := numbers.accumulate(new Variable(1), (a,b => a * b));
|
||||
|
||||
var concatenation := numbers accumulate(String new) with(:a:b)( a literal + b literal ).
|
||||
var concatenation := numbers.accumulate(new StringWriter(), (a,b => a.Printable + b.Printable));
|
||||
|
||||
console printLine(summary," ",product," ",concatenation).
|
||||
].
|
||||
console.printLine(summary," ",product," ",concatenation)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1 +1,3 @@
|
|||
for op in [+, -, *] println(reduce(op, 1:5)) end
|
||||
println([reduce(op, 1:5) for op in [+, -, *]])
|
||||
println([foldl(op, 1:5) for op in [+, -, *]])
|
||||
println([foldr(op, 1:5) for op in [+, -, *]])
|
||||
|
|
|
|||
|
|
@ -2,6 +2,6 @@ my @list = 1..10;
|
|||
say [+] @list;
|
||||
say [*] @list;
|
||||
say [~] @list;
|
||||
say [min] @list;
|
||||
say [max] @list;
|
||||
say min @list;
|
||||
say max @list;
|
||||
say [lcm] @list;
|
||||
|
|
|
|||
|
|
@ -11,26 +11,29 @@
|
|||
say ' LCM:' fold(@list, "LCM" )
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
fold: procedure; parse arg z; arg ,f; z=space(z); BIFs='MIN MAX LCM GCD'
|
||||
za=translate(z, f, ' '); zf=f"("translate(z, ',' , " ")')'
|
||||
if f=='+' | f=="*" then interpret "return" za
|
||||
if f=='||' then return space(z, 0)
|
||||
if f=='AVG' then interpret "return" fold(z, '+') "/" words(z)
|
||||
fold: procedure; parse arg z; arg ,f; z = space(z); BIFs= 'MIN MAX LCM GCD'
|
||||
za= translate(z, f, ' '); zf= f"("translate(z, ',' , " ")')'
|
||||
if f== '+' | f=="*" then interpret "return" za
|
||||
if f== '||' then return space(z, 0)
|
||||
if f== 'AVG' then interpret "return" fold(z, '+') "/" words(z)
|
||||
if wordpos(f, BIFs)\==0 then interpret "return" zf
|
||||
if f=='LIST' | f=="SHOW" then return z
|
||||
return 'illegal function:' arg(2)
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
GCD: procedure; $=; do j=1 for arg(); $=$ arg(j); end /*j*/
|
||||
parse var $ x z .; if x=0 then x=z /* [↑] build a list of arguments.*/
|
||||
x=abs(x)
|
||||
do k=2 to words($); y=abs(word($, k)); if y=0 then iterate
|
||||
do until _=0; _=x//y; x=y; y=_; end /*until*/
|
||||
end /*k*/
|
||||
GCD: procedure; $=; do j=1 for arg(); $= $ arg(j)
|
||||
end /*j*/
|
||||
parse var $ x z .; if x=0 then x= z /* [↑] build an arg list.*/
|
||||
x= abs(x)
|
||||
do k=2 to words($); y= abs( word($, k)); if y=0 then iterate
|
||||
do until _=0; _= x // y; x= y; y= _
|
||||
end /*until*/
|
||||
end /*k*/
|
||||
return x
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
LCM: procedure; $=; do j=1 for arg(); $=$ arg(j); end /*j*/
|
||||
x=abs(word($, 1)) /* [↑] build a list of arguments.*/
|
||||
do k=2 to words($); !=abs(word($, k)); if !=0 then return 0
|
||||
x=x*! / GCD(x, !) /*have GCD do the heavy lifting.*/
|
||||
end /*k*/
|
||||
LCM: procedure; $=; do j=1 for arg(); $= $ arg(j)
|
||||
end /*j*/
|
||||
x= abs(word($, 1)) /* [↑] build an arg list.*/
|
||||
do k=2 to words($); != abs(word($, k)); if !=0 then return 0
|
||||
x= x*! / GCD(x, !) /*GCD does the heavy work*/
|
||||
end /*k*/
|
||||
return x
|
||||
|
|
|
|||
5
Task/Catamorphism/VBA/catamorphism.vba
Normal file
5
Task/Catamorphism/VBA/catamorphism.vba
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
Public Sub reduce()
|
||||
s = [{1,2,3,4,5}]
|
||||
Debug.Print WorksheetFunction.Sum(s)
|
||||
Debug.Print WorksheetFunction.Product(s)
|
||||
End Sub
|
||||
Loading…
Add table
Add a link
Reference in a new issue