On 22 February 2010 16:42, Terrence Brannon <[email protected]> wrote: > What follows is a comparison of the Perl and Factor programs which generate > random text that looks like Latin.
Nice discussion! [...] > : getwords ( n -- array ) wordlist swap sample ; > > The Perl routine is named "words" yet it returns a string of words. I dont > think this is properly factored. I think this routine should've > returned an array of words. But I understand the author's intention. He > wanted to make a simple public API and most people are going to want a > string of words, not an array. > > Anyway, it is supposed to get n random words. In Factor's random > vocabulary, the sample word expects an array and a number IN THAT > ORDER. Because the number is already on the stack, we place the > wordlist on the stack and then call swap so they are in the right > order for sample and we are done. > > You almost wish there were some sort of stack-fiddling shorthand so that > getwords could be written as: > > : getwords ( n -- array ) wordlist sample(1,0) ; > > Where 2,1 indicates that positions 0 and 1 on the stack need to be > switched. The advantage of this approach is that you can specify complicated > reorderings with numbers instead of remembering the various shuffle words. > > Another idea is to overload the stack signature: > > : getwords ( n -swap- array ) wordlist sample ; > > Indicating that a swap should happen after wordlist is added to the stack. You can use the :: word (in the locals vocabulary) to have named parameters, in effect. You use it like this: :: getwords ( n -- array ) wordlist n sample ; The compiler optimizes this so it's just as efficient as the swap-based version. Paul. ------------------------------------------------------------------------------ Download Intel® 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 [email protected] https://lists.sourceforge.net/lists/listinfo/factor-talk
