MEMO: defines a word which caches results when invoked at runtime.
Since your word has no inputs, declaring it MEMO: will cause it to be
computed the first time it is invoked, and that result will be
returned immediately when the word has invoked.

So you could do this:

MEMO: wordlist ( -- array )  clean R/ \s+/ re-split [ >string ] map ;

A word can also be declared foldable, in which case the compiler will
replace calls where all inputs are literal with the result. Since your
word takes no inputs, declaring it foldable has the effect of calling
it and inserting the result at all call sites at compile time.

Finally, you can use CONSTANT: with $[, which performs parse-time evaluation:

CONSTANT: wordlist $[ clean R/ \s+/ re-split [ >string ] map ]

This approach is the dirtiest of all.

I'd recommend using MEMO: in this case.

Slava

On Tue, Feb 23, 2010 at 2:57 AM, Terrence Brannon <scheme...@gmail.com> wrote:
> I'm a bit confused on when to use MEMO: versus $[
>
> Practically speaking, below the words clean and wordlist need only be
> computed once, and I'm not sure of the best way to do it, or what the
> tradeoffs are of different approaches. I'm also not sure how to do it
> completely correctly. Is there an article on this subject I missed?
>
>
> USING: ascii kernel math.ranges random regexp sequences strings ;
> IN: lorem
>
>
> <PRIVATE
>
> CONSTANT: text "alias consequatur aut perferendis sit voluptatem
> accusantium doloremque aperiam, eaque ipsa quae ab illo inventore
> veritatis et."
>
> : clean ( -- string )  text >lower R/ [^\sA-Za-z]/ "" re-replace ;
>
> PRIVATE>
>
> : wordlist ( -- array )  clean R/ \s+/ re-split [ >string ] map ;
>
> : getword ( -- string )   wordlist random ;
> : getwords ( n -- array ) wordlist swap sample ;
> : sentencewords (  -- array  ) 4 10 [a,b] random getwords ;
> : sentencestring ( -- string ) sentencewords " " join ;
>
> : ucfirst ( string -- string ) 1 cut [ >upper ] dip append ;
> : sentence ( -- string ) sentencestring ucfirst "." append ;
> : sentences ( n -- array ) [ sentence ] replicate ;
> : paragraph ( -- string ) 3 7 [a,b] random sentences " " join ;
> : paragraphs ( n -- array ) [ paragraph ] replicate ;

------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to