I currently don't own RasperryPI. I got a PINE64 ROCK64 board that I can use for testing, it should give some info about performance. Stay tuned...
I would just use state map with last state and current state combined as map key and function to set new last state and new current state as map value (code in 8th, I don't know Python):Michiel O. wrote: ↑Sun Jul 14, 2019 12:08 pmIt implements the following example: a traffic light alternates between green and blue, but immediately jumps to red if an alarm button is pressed. After a while, the traffic light switches back to alternating green/blue.
Code: Select all
\ States
0 constant OFF
1 constant GREEN
2 constant BLUE
3 constant RED \ Alarm
OFF var, last-state
OFF var, current-state
: make-dword \ lword hword -- dword
0xffff n:band 16 n:shl swap
0xffff n:band
n:bor >s ;
: build-state-map \ -- m
m:new
OFF OFF make-dword ( OFF last-state ! GREEN current-state ! ) m:!
OFF GREEN make-dword ( GREEN last-state ! BLUE current-state ! ) m:!
BLUE GREEN make-dword ( GREEN last-state ! BLUE current-state ! ) m:!
GREEN BLUE make-dword ( BLUE last-state ! GREEN current-state ! ) m:!
GREEN RED make-dword ( RED last-state ! GREEN current-state ! ) m:!
RED GREEN make-dword ( GREEN last-state ! BLUE current-state ! ) m:!
RED BLUE make-dword ( BLUE last-state ! GREEN current-state ! ) m:!
BLUE RED make-dword ( RED last-state ! BLUE current-state ! ) m:! ;
build-state-map var, state-map
[ "OFF", "GREEN", "BLUE", "RED"] var, colors
\ Demo starts here
: app:main
(
last-state @ current-state @ make-dword state-map @
case
colors @
current-state @
caseof
. cr
) 3 times
current-state @ last-state !
RED current-state !
colors @
current-state @
caseof
. cr
(
last-state @ current-state @ make-dword state-map @
case
colors @
current-state @
caseof
. cr
) 2 times
current-state @ last-state !
RED current-state !
colors @
current-state @
caseof
. cr
(
last-state @ current-state @ make-dword state-map @
case
colors @
current-state @
caseof
. cr
) 4 times
bye ;
Code: Select all
GREEN
BLUE
GREEN
RED
GREEN
BLUE
RED
BLUE
GREEN
BLUE
GREEN
I just tested on my ROCK64 board using american-english wordlist and following 8th code:
Code: Select all
m:new var, anamap
a:new var, anakeys
: s:sort \ s -- s
null s:/
' s:cmpi a:sort
"" a:join ;
: process-words \ word --
dup /^[a-z]+$/ r:match nip if
dup
>r
s:sort
anamap @
over
m:exists? if
over m:@ r> a:push rot swap m:!
else
swap a:new r> a:push m:!
then
drop
else
drop
then ;
: read-and-check-words
"/usr/share/dict/words"
f:open-ro
' process-words f:eachline
f:close ;
: len< \ key --
drop ;
: len>= \ key --
anakeys @ swap a:push drop ;
: fetch-ana-list \ key array --
a:len 2 n:cmp
1 n:+
nip
[ ' len< , ' len>= , ' len>= ]
swap
caseof ;
: key-val-cmp \ key1 key2
anamap @ swap m:@ \ key1 anamap key2val
0 a:@ nip \ key1 anamap val2
swap rot \ val2 anamap key1
m:@ nip \ val2 key1val
0 a:@ nip \ val2 val1
swap \ val1 val2
s:cmp ;
: sort-keys-by-first-word
anakeys @ ' key-val-cmp a:sort drop ;
: list-words \ ix value --
anamap @ swap m:@ nip ' s:cmp a:sort
" " a:join . cr ;
: app:main
read-and-check-words
anamap @ ' fetch-ana-list m:each drop
sort-keys-by-first-word
anakeys @ ' list-words a:each! drop
anakeys @ a:len nip "\nAnagrams: %d\n" s:strfmt .
bye ;
Code: Select all
real 0m1,667s
user 0m1,496s
sys 0m0,148s
Actually, I implemented sorting, it's described in https://www.raspberrypi.org/forums/view ... 0#p1499688scruss wrote: ↑• Though you're not doing half the work that my example did: you include proper nouns and possessives and smash everything to lower case: you need to pick out the words that are just lower case alphanumerics.Michiel O. wrote: ↑ I did the same, in Python3, using /usr/share/dict/words (2.5M file, 1 second runtime):
• Your output isn't sorted.
Indeed, you can install COBOL on your Raspberry Pi:
Code: Select all
IDENTIFICATION DIVISION.
PROGRAM-ID. Hello.
ENVIRONMENT DIVISION.
DATA DIVISION.
PROCEDURE DIVISION.
DISPLAY "Hello, " WITH NO ADVANCING
DISPLAY "world!"
STOP RUN.
Code: Select all
IDENTIFICATION DIVISION.
PROGRAM-ID. FIBO.
DATA DIVISION.
LOCAL-STORAGE SECTION.
01 REPORTLINE.
05 FILLER PIC X(17) VALUE 'Fibonacci number '.
05 FIBONACCI-ORDER PIC 9(6).
05 FILLER PIC X(4) VALUE ' is '.
05 RESULT PIC 9(6).
01 GLOBAL-WORKING-VARIABLES.
05 BUCKET-1 PIC 9(6).
05 BUCKET-2 PIC 9(6).
05 BUCKET-SUM PIC 9(7).
05 NEEDED-ITERATIONS PIC 9(6).
PROCEDURE DIVISION.
MAIN SECTION.
PERFORM CALCULATE-FIBONACCI-NUMBER
VARYING FIBONACCI-ORDER FROM 2 BY 1
UNTIL FIBONACCI-ORDER > 10.
STOP RUN.
CALCULATE-FIBONACCI-NUMBER SECTION.
MOVE 0 TO BUCKET-1.
MOVE 1 TO BUCKET-2.
COMPUTE NEEDED-ITERATIONS = FIBONACCI-ORDER - 1.
PERFORM FIBONACCI-ITERATION NEEDED-ITERATIONS TIMES.
MOVE BUCKET-SUM TO RESULT.
DISPLAY REPORTLINE.
FIBONACCI-ITERATION SECTION.
ADD BUCKET-1 TO BUCKET-2 GIVING BUCKET-SUM.
MOVE BUCKET-2 TO BUCKET-1.
MOVE BUCKET-SUM TO BUCKET-2.
END PROGRAM FIBO.
Been there, done that and no wish to return to itMichiel O. wrote: ↑Sun Jul 14, 2019 9:25 pm...
☞ Would you like to be a COBOL programmer? I heard you can make good money with it!
A very good question...Not intending to pick on you, because I seem to see that type of code everywhere. But why that, a While(True) and a Break to escape that loop rather than ...
Code: Select all
while (1) {
char c = readInput();
if (c == 'x') {
// Do something
...
} else if (c == 'y') {
// Do something
...
} else if (c == 'z') {
// Do something
...
} else if (c == 0) {
// We are done.
break;
} else {
// Do the default thing.
...
}
}
You can sometimes say "do ....... while( --n != 0 );" and it wont bother to do the test, it will just use the zero flag set by the subtract.
I totally agree. As we must have discussed a few times before I think. I will favor clarity and aesthetics over performance even, if the hit is not significant.However, in general nowadays, I would rather get the code simple, easy to read, and correct, first, then let the compiler worry about efficiency details.
Thanks for providing the explanation.
Code: Select all
done = False
while not done:
if ...
else:
done = True
I have never understood why languages where that's considered fundamentally useful don't have 'forever { ... }' or 'do { ... } forever' constructs built-in, that there's no standard way of specifying such things, programmers being left to their own implementations of the same.
Yeah, that is annoying.I have never understood why languages where that's considered fundamentally useful don't have 'forever { ... }' or 'do { ... } forever' constructs built-in, that there's no standard way of specifying such things, programmers being left to their own implementations of the same.
Code: Select all
forever:
// ...
// ...
goto forever;
As I said, the language B did have onehippy wrote: ↑Mon Jul 15, 2019 12:33 pmI have never understood why languages where that's considered fundamentally useful don't have 'forever { ... }' or 'do { ... } forever' constructs built-in, that there's no standard way of specifying such things, programmers being left to their own implementations of the same.
Code: Select all
repeat
{
...
}