Paste: Good words to get to know

Author: noenzyme
Mode: text
Date: Fri, 5 Jun 2009 06:34:38
Plain Text |
I haven't found a good list of vocabs/words to look at but below are some of the ones I find I use often:

1array, 2array, 3array -> arrays (Arrays) 
length, first, first2, first3, nth, head, tail, append, suffix, prefix, concat -> sequences (Sequence operations)
map, map-index, each, each-index -> sequences (Sequence combinators) 
dip, 2dip, keep, swap, dup, over, nip, tuck, drop, 2drop -> kernel (kernel vocabulary)
if, when, unless, and, or, not -> kernel (Conditionals and logic)
bi, tri -> kernel (Cleave combinators)
output>array -> combinator.smart (Smart combinators)
'[ _  @ ]  -> fry syntax (Fried quotations), short cut for curry (curry ( obj quot -- curry ))
SYMBOL: -> variables (SYMBOL:)
TUPLE: -> classes (Classes)
>>slot1, slot1>> -> instance variables called accessors in factor (Slot accessors)
GENERIC:, M: -> methods (Generic words and methods)

The bi combinator is one of the most used words. It is a clear way of indicating you want to do 2 things to the value on the top of the stack. Say of instance we wanted to pull out two slots from an object:

TUPLE: foo slot1 slot2

: add-slots-1 ( foo -- int )
   dup slot1>> swap slot2>> + ;

: add-slots-2 ( foo -- int )
   [ slot1>> ] keep slot2>> + ;

: add-slots-3 ( foo -- int )
   [ slot1>> ] [ slot2>> ] bi + ;

The first version uses only shuffle words, there you have to manipulate the stack in your head to read along. Version two you can see you are doing something to the top of the stack, but then also keeping a copy of it around making it easier to follow. The last version uses bi, a cleave combinator, to indicate you are going to do two things with the object on the top of the stack making it very nice to follow. There is also tri for to do three things and cleave to do n things.

fry and map are best buddies for manipulating all things in an array. Map iterates of a sequence and applies a quotation to each element. Fry allows you to specify at run time what you want to do. For example say we want to add a number to each number in an array:

The compile time version:

: my-add1 ( seq -- seq' )
   [ 1+ ] map ;

: my-add2 (seq -- seq' )
  [ 2 + ] map ;

etc ..

The run time version:

: my-add ( seq n -- seq' )
  '[ _ + ] map ;

And finally the old school curry version:

: my-add-curry ( seq n -- seq' )
  [ + ] curry map ;

Both fry, and bi are conventions invented in the last 6 months but they have made the language much cleaner. 

New Annotation

Summary:
Author:
Mode:
Body: