On 5-Dec-07, at 5:41 PM, Cyril Slobin wrote:
> I am a total newbee in factor (have just written my first sctipt).
> I hope this list is right place for dumb questions. If it is not,
> redirect me to the appropriate place, please!
This is the right place for any type of Factor question. You can also
ask questions on IRC, sometimes you'll get a faster response there.
> The problem solved with this my script was trivial: count all
> different
> words in text and print counters in alphabetical order. My solution
> was:
Don't forget your USING: and your IN:.
> : process-line ( assoc str -- assoc )
> " " split [ 1 swap pick at+ ] each ;
>
> : collect-data ( assoc -- assoc )
> readln [ process-line collect-data ] when* ;
>
> : read-input ( -- assoc )
> 100003 <hashtable>
> collect-data
> "" over delete-at ;
>
> : write-output ( assoc -- )
> dup keys natural-sort
> [ dup write bl over at number>string write nl ] each
> drop ;
This is more idiomatic:
: write-output ( assoc -- )
>alist sort-keys [ >r write bl r> number>string print ] assoc-
each ;
>
> read-input write-output
>
> After this first attempt a lot of question arises:
>
> 1. Is this a good way to code in factor? Maybe I have missed something
> obvious to others but obscure to me?
Yes. Another approach:
: tally ( seq -- assoc )
H{ } clone [ [ 1 -rot at+ ] curry each ] keep ;
: read-words ( stream -- seq )
contents " \t\r\n" split ;
: read-input ( -- assoc )
stdio get read-words tally ;
The advantage of this is that you get two reusable words, 'tally' and
'read-words'.
> 2. I am still not comfortable with all this compilation, inlining,
> stack
> effects inference an all this stuff. As far as I have understood,
> a compiled version can run a magnitude faster than interpreted one.
> Maybe something in my script prevents it from compilation?
If you're using 0.90, the I/O does.
> 2a. factor says "unable to infer stack effect" of readln and write.
> Why?
Because in 0.90 I/O did not compile.
> 3. When calling the script with "factor -script test.factor < in >
> out",
> it works as expected but prints "Compiling 4 words..." to stdout
> before any real output. Is this disable-able? The same for GC
> messages
> to stderr.
I fixed the compiling message; it's omitted when using -script now.
As for GC messages, you can redirect stderr to /dev/null.
> 4. After looking at examples in source directory, I conclude that
> idiomatic
> way to write a loop is tail recursion. Is this true? Why there
> is no
> "while" word and its counterparts? I guess it is for a reason...
> Or they
> exists and I have just missed them in documentation?
In 0.91 there is a 'while' word but it is almost the same as a tail
recursion:
[ A ] [ B ] [ C ] while
: foo A [ B foo ] [ C ] if ;
However in most cases you don't write loops explicitly, you use
combinators like 'each', 'map', etc.
> 4a. If integers even can be iterated by "each" (very enlightening
> feature,
> I wish it be in python), why linereaders aren't? Or they are
> really?
"foo.txt" <file-reader> lines [
....
] each
You can use 'lines' to get a sequence of lines.
Slava
-------------------------------------------------------------------------
SF.Net email is sponsored by: The Future of Linux Business White Paper
from Novell. From the desktop to the data center, Linux is going
mainstream. Let it simplify your IT future.
http://altfarm.mediaplex.com/ad/ck/8857-50307-18918-4
_______________________________________________
Factor-talk mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/factor-talk