September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,46 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace RosettaGenerator
|
||||
{
|
||||
class ClosureStyle
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Func<int> squaresGenerator = PowerGeneratorAsClosure(2);
|
||||
Func<int> cubesGenerator = PowerGeneratorAsClosure(3);
|
||||
|
||||
Func<int> filter = FilterAsClosure(squaresGenerator, cubesGenerator);
|
||||
|
||||
foreach (int i in Enumerable.Range(0, 20))
|
||||
filter();
|
||||
foreach (int i in Enumerable.Range(21, 10))
|
||||
Console.Write(filter() + " ");
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
public static Func<int> PowerGeneratorAsClosure(int exponent)
|
||||
{
|
||||
int x = 0;
|
||||
return () => { return (int)Math.Pow(x++, exponent); };
|
||||
}
|
||||
|
||||
public static Func<int> FilterAsClosure(Func<int> squaresGenerator, Func<int> cubesGenerator)
|
||||
{
|
||||
int squareValue;
|
||||
int cubeValue = cubesGenerator();
|
||||
return () =>
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
squareValue = squaresGenerator();
|
||||
while (squareValue > cubeValue)
|
||||
cubeValue = cubesGenerator();
|
||||
if (squareValue < cubeValue)
|
||||
return squareValue;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,46 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace RosettaGenerator
|
||||
{
|
||||
class ListStyle
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
IEnumerator<int> squaresGenerator = PowerGeneratorAsList(2);
|
||||
IEnumerator<int> cubesGenerator = PowerGeneratorAsList(3);
|
||||
|
||||
IEnumerable<int> filter = FilterAsList(squaresGenerator, cubesGenerator);
|
||||
|
||||
foreach (int value in filter.Skip(20).Take(10))
|
||||
Console.Write(value + " ");
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
public static IEnumerator<int> PowerGeneratorAsList(int exponent)
|
||||
{
|
||||
int x = 0;
|
||||
while (true)
|
||||
yield return (int)Math.Pow(x++, exponent);
|
||||
}
|
||||
|
||||
public static IEnumerable<int> FilterAsList(IEnumerator<int> squaresGenerator, IEnumerator<int> cubesGenerator)
|
||||
{
|
||||
int squareValue;
|
||||
int cubeValue = cubesGenerator.Current;
|
||||
while (true)
|
||||
{
|
||||
squareValue = squaresGenerator.Current;
|
||||
while (squareValue > cubeValue)
|
||||
{
|
||||
cubesGenerator.MoveNext();
|
||||
cubeValue = cubesGenerator.Current;
|
||||
}
|
||||
if (squareValue < cubeValue)
|
||||
yield return squareValue;
|
||||
squaresGenerator.MoveNext();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,10 +0,0 @@
|
|||
529
|
||||
576
|
||||
625
|
||||
676
|
||||
784
|
||||
841
|
||||
900
|
||||
961
|
||||
1024
|
||||
1089
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
function* nPowerGen(n) {
|
||||
let e = 0;
|
||||
while (1) { e++ && (yield Math.pow(e, n)); }
|
||||
}
|
||||
|
||||
function* filterGen(gS, gC, skip=0) {
|
||||
let s = 0; // The square value
|
||||
let c = 0; // The cube value
|
||||
let n = 0; // A skip counter
|
||||
|
||||
while(1) {
|
||||
s = gS.next().value;
|
||||
s > c && (c = gC.next().value);
|
||||
s == c ?
|
||||
c = gC.next().value :
|
||||
n++ && n > skip && (yield s);
|
||||
}
|
||||
}
|
||||
|
||||
const filtered = filterGen(nPowerGen(2), nPowerGen(3), skip=20);
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
// Generate the first 10 values
|
||||
for (let n = 0; n < 10; n++) {
|
||||
console.log(filtered.next().value)
|
||||
}
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
// version 1.1.0
|
||||
// compiled with flag -Xcoroutines=enable to suppress 'experimental' warning
|
||||
|
||||
import kotlin.coroutines.experimental.buildSequence
|
||||
|
||||
fun generatePowers(m: Int) =
|
||||
buildSequence {
|
||||
var n = 0
|
||||
val mm = m.toDouble()
|
||||
while (true) yield(Math.pow((n++).toDouble(), mm).toLong())
|
||||
}
|
||||
|
||||
fun generateNonCubicSquares(squares: Sequence<Long>, cubes: Sequence<Long>) =
|
||||
buildSequence {
|
||||
val iter2 = squares.iterator()
|
||||
val iter3 = cubes.iterator()
|
||||
var square = iter2.next()
|
||||
var cube = iter3.next()
|
||||
while (true) {
|
||||
if (square > cube) {
|
||||
cube = iter3.next()
|
||||
continue
|
||||
} else if (square < cube) {
|
||||
yield(square)
|
||||
}
|
||||
square = iter2.next()
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val squares = generatePowers(2)
|
||||
val cubes = generatePowers(3)
|
||||
val ncs = generateNonCubicSquares(squares, cubes)
|
||||
print("Non-cubic squares (21st to 30th) : ")
|
||||
ncs.drop(20).take(10).forEach { print("$it ") } // print 21st to 30th items
|
||||
println()
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
g = List(1); \\ generator stack
|
||||
|
||||
genpow(p) = my(a=g[1]++);listput(g,[0,p]);()->g[a][1]++^g[a][2];
|
||||
|
||||
genpowf(p,f) = my(a=g[1]++);listput(g,[0,p]);(s=0)->my(q);while(ispower(p=g[a][1]++^g[a][2],f)||(s&&q++<=s),);p;
|
||||
42
Task/Generator-Exponential/Phix/generator-exponential.phix
Normal file
42
Task/Generator-Exponential/Phix/generator-exponential.phix
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
--
|
||||
-- demo\rosetta\Generator_Exponential.exw
|
||||
-- ======================================
|
||||
--
|
||||
bool terminate = false
|
||||
|
||||
atom res
|
||||
|
||||
procedure powers(integer p)
|
||||
integer i=0
|
||||
while not terminate do
|
||||
res = power(i,p)
|
||||
task_suspend(task_self())
|
||||
task_yield()
|
||||
i += 1
|
||||
end while
|
||||
end procedure
|
||||
|
||||
constant squares = task_create(routine_id("powers"),{2}),
|
||||
cubes = task_create(routine_id("powers"),{3})
|
||||
|
||||
atom square, cube
|
||||
task_schedule(cubes,1)
|
||||
task_yield()
|
||||
cube = res
|
||||
for i=1 to 30 do
|
||||
while 1 do
|
||||
task_schedule(squares,1)
|
||||
task_yield()
|
||||
square = res
|
||||
while cube<square do
|
||||
task_schedule(cubes,1)
|
||||
task_yield()
|
||||
cube = res
|
||||
end while
|
||||
if square!=cube then exit end if
|
||||
end while
|
||||
if i>20 then
|
||||
?square
|
||||
end if
|
||||
end for
|
||||
terminate = 1
|
||||
|
|
@ -1,40 +1,36 @@
|
|||
/*REXX program demonstrates how to use a generator (also known as iterators).*/
|
||||
parse arg N .; if N=='' then N=20 /*N not specified? Then use default.*/
|
||||
@.= /* [↓] calculate squares,cubes,pureSq.*/
|
||||
do j=1 for N; call Gsquare j
|
||||
call Gcube j
|
||||
call GpureSquare j /*these are cube─free squares.*/
|
||||
end /*j*/
|
||||
/*REXX program demonstrates how to use a generator (also known as an iterator). */
|
||||
parse arg N .; if N=='' | N=="," then N=20 /*N not specified? Then use default.*/
|
||||
@.= /* [↓] calculate squares,cubes,pureSq.*/
|
||||
do i=1 for N; call Gsquare i
|
||||
call Gcube i
|
||||
call GpureSquare i /*these are cube─free square numbers.*/
|
||||
end /*i*/
|
||||
|
||||
do k=1 for N; @.pureSquare.k=; end /*k*/ /*dropping 1st N values.*/
|
||||
do k=1 for N; @.pureSquare.k=; end /*k*/ /*this is used to drop 1st N values.*/
|
||||
|
||||
w=length(N+10); ps='pure square' /*width of the numbers. */
|
||||
w=length(N+10); ps= 'pure square' /*the width of the numbers; a literal.*/
|
||||
|
||||
do m=N+1 for 10; say ps right(m, w)":" right(GpureSquare(m), 3*w)
|
||||
end /*m*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
Gpower: procedure expose @.; parse arg x,p; q=@.pow.x.p
|
||||
if q\=='' then return q; _=x**p
|
||||
if pos(.,_)\==0 then do
|
||||
parse var _ 'E' e; numeric digits e+5; _=x**p
|
||||
end /* [↑] re─calculate with more digits.*/
|
||||
@.pow.x.p=_
|
||||
if q\=='' then return q; _=x**p; @.pow.x.p=_
|
||||
return _
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
Gsquare: procedure expose @.; parse arg x; q=@.square.x
|
||||
if q=='' then @.square.x=Gpower(x,2)
|
||||
if q=='' then @.square.x=Gpower(x, 2)
|
||||
return @.square.x
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
Gcube: procedure expose @.; parse arg x; q=@.cube.x
|
||||
if q=='' then @.cube.x=Gpower(x,3); _=@.cube.x; @.3pow._=1
|
||||
if q=='' then @.cube.x=Gpower(x, 3) _=@.cube.x; @.3pow._=1
|
||||
return @.cube.x
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
GpureSquare: procedure expose @.; parse arg x; q=@.pureSquare.x
|
||||
if q\=='' then return q
|
||||
#=0
|
||||
do j=1 until #==x; ?=Gpower(j,2) /*search for pure square*/
|
||||
if @.3pow.?==1 then iterate /*is it a power of 3 ? */
|
||||
#=#+1; @.pureSquare.#=? /*assign next pureSquare*/
|
||||
do j=1 until #==x; ?=Gpower(j, 2) /*search for pure square. */
|
||||
if @.3pow.?==1 then iterate /*is it a power of three? */
|
||||
#=#+1; @.pureSquare.#=? /*assign next pureSquare. */
|
||||
end /*j*/
|
||||
return @.pureSquare.x
|
||||
|
|
|
|||
|
|
@ -1,27 +0,0 @@
|
|||
(define (power-seq n)
|
||||
(let ((i 0))
|
||||
(lambda ()
|
||||
(set! i (+ 1 i))
|
||||
(expt i n))))
|
||||
|
||||
(define (filter-seq m n)
|
||||
(let* ((s1 (power-seq m)) (s2 (power-seq n))
|
||||
(a 0) (b 0))
|
||||
(lambda ()
|
||||
(set! a (s1))
|
||||
(let loop ()
|
||||
(if (>= a b) (begin
|
||||
(cond ((> a b) (set! b (s2)))
|
||||
((= a b) (set! a (s1))))
|
||||
(loop))))
|
||||
a)))
|
||||
|
||||
(let loop ((seq (filter-seq 2 3)) (i 0))
|
||||
(if (< i 30)
|
||||
(begin
|
||||
(if (> i 20)
|
||||
(begin
|
||||
(display (seq))
|
||||
(newline))
|
||||
(seq))
|
||||
(loop seq (+ 1 i)))))
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
576
|
||||
625
|
||||
676
|
||||
784
|
||||
841
|
||||
900
|
||||
961
|
||||
1024
|
||||
1089
|
||||
13
Task/Generator-Exponential/Zkl/generator-exponential-1.zkl
Normal file
13
Task/Generator-Exponential/Zkl/generator-exponential-1.zkl
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
fcn powers(m){ n:=0.0; while(1){vm.yield(n.pow(m).toInt()); n+=1} }
|
||||
var squared=Utils.Generator(powers,2), cubed=Utils.Generator(powers,3);
|
||||
|
||||
fcn filtered(sg,cg){s:=sg.next(); c:=cg.next();
|
||||
while(1){
|
||||
if(s>c){c=cg.next(); continue;}
|
||||
else if(s<c) vm.yield(s);
|
||||
s=sg.next()
|
||||
}
|
||||
}
|
||||
var f=Utils.Generator(filtered,squared,cubed);
|
||||
f.drop(20);
|
||||
f.walk(10).println();
|
||||
10
Task/Generator-Exponential/Zkl/generator-exponential-2.zkl
Normal file
10
Task/Generator-Exponential/Zkl/generator-exponential-2.zkl
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
fcn powers(m){[0.0..].tweak(fcn(n,m){a:=n; do(m-1){a*=n} a}.fp1(m))}
|
||||
var squared=powers(2), cubed=powers(3);
|
||||
|
||||
fcn filtered(sg,cg){s:=sg.peek(); c:=cg.peek();
|
||||
if(s==c){ cg.next(); sg.next(); return(self.fcn(sg,cg)) }
|
||||
if(s>c) { cg.next(); return(self.fcn(sg,cg)); }
|
||||
sg.next(); return(s);
|
||||
}
|
||||
var f=[0..].tweak(filtered.fp(squared,cubed))
|
||||
f.drop(20).walk(10).println();
|
||||
Loading…
Add table
Add a link
Reference in a new issue