[REBOL] Re: byte frequencies
From: larry:ecotope at: 5-Jul-2001 21:13
Hi Joel,
Good work. Here is my slightly more Rebolian revision:
bytefreq2: func [fi [file!] /local ft cnt] [
ft: array/initial 256 0
foreach ch read fi [
ch: 1 + ch
poke ft ch 1 + pick ft ch
]
repeat ch length? ft [
if 0 < cnt: pick ft ch [
print [mold to-char ch - 1 tab cnt]
]
]
]
A few quick comments. In your original the variable CH does not have to be
declared local to BYTEFREQ because it is only used as an arg (which are
always local to their functions) for the FOREACH and FOR functions. TO-FILE
is superfluous because fi was declared as file! in the function spec.
Inside the code block of a function the args can be redefined as convenient,
in particular in the FOREACH function, CH can be given any appropriate value
in the code block. It is reset to the correct value before each execution of
the code block.
I rearranged things to eliminate the redundant index calculations. The
REPEAT construction I used guarantees that one automatically indexes through
all elements of the series argument.
I added CNT as a local variable, but we could also have used the variable
LOCAL if we wished to avoid declaring another local at the expense of
readability.
Your basic idea of using implicit type-conversion to allow characters to be
used as indices is clever. But the reader should note that when adding
characters and integers, the result is order dependent:
>> 1 + #"a"
== 98
>> #"a" + 1
== #"b"
>>
Cheers
-Larry