Factor/NeophyteFAQ

LISTENER

LOOK here if you are doing something in the Listener and having issues

How to prettyprint the contents of the stack without destroying it

The word for that is .s

I'm trying to get these SYMBOL: and I'm just getting back f

in your file, set them with set-global, instead of set

Anything set with set will get lost when the top level expressions finish evaluating since files are parsed inside a with-scope

I keep USE:ing source files from the listener but when I play with them in the listener, they do not have the search paths that the source file loaded!

The only way to change the listener's search path is by entering USING:, USE:, etc in the listener

the first time, USE: will load the vocabulary, but any USE: or USING: in the source file that gets loaded will not affect the listener

slava: methods don't belong to a vocabulary

slava: methods belong to a generic word

slava: to invoke a generic word, you have to have the generic word's vocabulary in your search path

metaperl: I think that might trip up a lot of people

slava: well, its how java import statements owrk

slava: it only affects the source file being parsed

slava: I guess python and ruby just stick everything in one global namespace

slava: so you load a source file which loads another source file and suddently you've got 30 new identifiers in the global scope, some of which clash with your existing ones :)

I keep killing the listener and restarting to make sure all my definitions get refreshed. Is there a simpler way?

If you just want to reload the source file, do refresh-all in the listener

you don't have to restart it

PROGRAMMING

How many ways to create an instance of a TUPLE:

USE: constructors   CONSTRUCTOR: employee ( fname lname rate -- obj ) ;  employee-data [ [ <employee> ] input<array ] map

boa

new

T{

I want to create a sequence consisting of the chars from a to z

USING: math.ranges ; CHAR: a CHAR: z [a,b]

26 iota [ CHAR: a + ] map

Templated text generation

Here are some ideas:

[ "(" "3" "4" "5" ")" "2" "4" "4" "-" ] { } output>sequence

[ "(" , 3 [ random-digit ] replicate ] { } make

[ call( -- obj ) ] map

"(ddd)ddd-dddd" [ { { CHAR: d [ 10 random number>string ] } [ 1string ] } case ] { } map-as concat

: reader-loop ( -- )
    read1 [
        dup CHAR: \ = [
            drop read1
            { { CHAR: d [ CHAR: 0 10 random + write1 ] } } case
        ] [ write1 ] if
        reader-loop
    ] when* ;

: convert ( str -- str' )
    [ [ reader-loop ] with-string-writer ] with-string-reader ;

  [
     [  "(" ]
     [ random-digit ]
     [ random-digit ]
     [ random-digit ]
     [  ") " ]
     [ random-digit ]
     [ random-digit ]
     [ random-digit ]
     [ "-" ]
     [ random-digit ]
     [ random-digit ]
     [ random-digit ]
     [ random-digit ]
] call( -- o )

I want to subtract 18 years from todays date and display that date

now 18 years time- .

How do I create an array with 10 random numbers in it?

10 [ 100 random ] replicate

Say I have '5' on the stack, and I want to create a sequence { 1 x 3 } and place 5 where the x is, what's the best way to do this?

slava: 5 1 swap 3 3array

slava: or 5 :> x { 1 x 3 }

slava: prunedtree would suggest something like 5 1array { 1 } { 3 } surround [ ] map

DATA STRUCTURES

How do I make a literal string

DELIMITED:

TESTING

How do you write tests for a vocab that generates random text?

Notwithstanding Gertm's suggestion that you should write them with luck, you can have tests assert that the result is a string, of the right length, etc.

How about seed-random? Don't use that word. But you can do this:

1234 <mersenne-twister> [ ... ] with-random

Now a sequence of calls to 'random' will produce the same results every time inside the quotation.

THE SOFTWARE SYSTEM

Is the vocabulary system hierarchical?

there's hierarchy support. USE: foo.bar loads foo/bar/bar.factor

This revision created on Mon, 1 Mar 2010 21:23:21 by metaperl (=== How many ways to create an instance of a TUPLE: ===)