Révision | 1243c3ba5627ec22a957a30299c1f1e56b1abb86 (tree) |
---|---|
l'heure | 2024-04-12 06:25:37 |
Auteur | Albert Mietus < albert AT mietus DOT nl > |
Commiter | Albert Mietus < albert AT mietus DOT nl > |
Aligned 'TheSieve blog code' with (manual) RPy code [See: Sieve_in_rPython/basic-1]
@@ -8,6 +8,12 @@ | ||
8 | 8 | implement Generator { |
9 | 9 | int maxValue; |
10 | 10 | |
11 | + init() | |
12 | + { | |
13 | + .maxValue = -1; | |
14 | + } | |
15 | + | |
16 | + | |
11 | 17 | StartSieve.newMax(max) on .controll |
12 | 18 | { |
13 | 19 | .maxValue := max; |
@@ -21,6 +27,7 @@ | ||
21 | 27 | i:=0; |
22 | 28 | while (i < .maxValue) { |
23 | 29 | .outlet.input(i); |
30 | + i +=1; | |
24 | 31 | } |
25 | 32 | } |
26 | 33 |
@@ -53,6 +60,11 @@ | ||
53 | 60 | implement Finder { |
54 | 61 | int count; |
55 | 62 | |
63 | + init() | |
64 | + { | |
65 | + .count = 0; | |
66 | + } | |
67 | + | |
56 | 68 | SimpleSieve.input(foundPrime) on .newPrime |
57 | 69 | { |
58 | 70 | report(foundPrime) |
@@ -10,13 +10,11 @@ | ||
10 | 10 | sub generator; |
11 | 11 | sub finder; |
12 | 12 | alias lastSieve; // The list of Sieve's grow dynamically!; keep track of the last one |
13 | - ... | |
14 | 13 | |
15 | 14 | |
16 | 15 | .. code-tab:: ReasonML init() |
17 | 16 | |
18 | - init() | |
19 | - { | |
17 | + init() { | |
20 | 18 | super.init(); |
21 | 19 | |
22 | 20 | .generator := Generator(); |
@@ -28,38 +26,53 @@ | ||
28 | 26 | } |
29 | 27 | |
30 | 28 | |
31 | - .. code-tab:: ReasonML try() | |
29 | + .. code-tab:: ReasonML finder.found | |
32 | 30 | |
33 | - // We have extend the sieve-list (and reconnect) on a newly found prime .. | |
34 | - SimpleSieve.try(newPrime) on .finder.found | |
31 | + // We have extend the sieve-list (and reconnect), for every newly found prime. | |
32 | + SimpleSieve.input(newPrime) on .finder.found | |
35 | 33 | { |
36 | 34 | alias s; |
37 | 35 | |
38 | 36 | // Extent the sieve-list ... |
39 | 37 | s:= Sieve(newPrime); |
40 | 38 | insert_sieve(s); |
39 | + verifyCorrectPrime(newPrime); | |
41 | 40 | } |
42 | 41 | |
42 | + .. code-tab:: ReasonML local functions | |
43 | 43 | |
44 | - insert_sieve(alias:s) | |
45 | - { | |
46 | - // Connect s to the lastSieve, or the Generator | |
47 | - if (.lastSieve == Ground) { // .lastSieve == Ground, so not connected, so we have the first Sieve to connect to .generator | |
44 | + insert_sieve(alias:s) { | |
45 | + // Connect the input of S to the lastSieve, or to the Generator | |
46 | + if (.lastSieve == Ground) { // .lastSieve == Ground, so not connected, so we have the first Sieve to connect to .generator | |
48 | 47 | .generator.outlet = s.try; |
49 | 48 | } else { |
50 | 49 | .lastSieve.coprime = s.try; |
51 | 50 | } |
52 | 51 | |
53 | - // And to the Finder, that is self | |
54 | - s.coprime = .finder.newPrime; | |
52 | + s.coprime = .finder.newPrime; // Connect to output of S to the finder | |
53 | + .lastSieve := s // a new lastSieve | |
55 | 54 | } |
56 | 55 | |
56 | + verifyCorrectPrime(int:gotPrime) { | |
57 | + local int count:=0; ///Semantics: instance var, with function-local scope | |
58 | + cons PRIMES:= [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]; | |
59 | + | |
60 | + if count >= len(PRIMES) { | |
61 | + print("Warning: Can only check {len(PRIMES)} primes, {gotPrime) is not verified") | |
62 | + return | |
63 | + } | |
64 | + | |
65 | + expect := PRIMES[count] | |
66 | + if expect != gotPrime { | |
67 | + printf("ERROR: Prime no {count} should be {expect}, but got {gotPrime}”) | |
68 | + | |
69 | + count += 1; | |
70 | + | |
71 | + | |
57 | 72 | .. code-tab:: ReasonML powerOn() |
58 | 73 | |
59 | - powerOn() // this kick-starts “every main” | |
74 | + powerOn(int:max=10) // this kick-starts “every main” | |
60 | 75 | { |
61 | - int max := 10; | |
62 | - | |
63 | 76 | .generator.runTo(max); |
64 | 77 | } |
65 | 78 |
@@ -46,9 +46,8 @@ | ||
46 | 46 | send_proto -> DispatchTable |
47 | 47 | |
48 | 48 | class connection { |
49 | - + out: <outport, component> | |
50 | - + in: <inport, component> | |
51 | - # protocol <indirect> | |
49 | + + outport: <outport, component> | |
50 | + + inport: <inport, component> | |
52 | 51 | } |
53 | 52 | note top #aqua |
54 | 53 | This is the result of |
@@ -89,7 +88,6 @@ | ||
89 | 88 | |
90 | 89 | Event "1".. "*" EventHandler |
91 | 90 | Event .> Protocol: > part of |
92 | - Protocol ... connection | |
93 | 91 | |
94 | 92 | @enduml |
95 | 93 |