When this was posted on another site, I translated the code examples from the slides into Factor. So . . . here they are!
This time I’m leaving out the import statements, for brevity. All the line breaks and indentations are optional.
: add1 ( x -- x' ) 1 + ; : square ( x -- x' ) dup * ; : double ( x -- x' ) 2 * ; : demo1 ( -- n ) 5 add1 square double ; : add ( x y -- n ) + ; : times ( x y -- n ) * ; : demo2 ( -- n ) 5 1 add 2 times ;
: pipeline ( seq -- n ) [ 2 + ] [ 3 > ] map-filter length ;
: (fizz-buzz) ( i -- str ) dup [ 3 divisor? ] [ 5 divisor? ] bi 2dup or [ [ "Fizz" "" ? ] [ "Buzz" "" ? ] bi* append nip ] [ 2drop number>string ] if ; : fizz-buzz ( -- ) 35 [1..b] [ (fizz-buzz) ] map "," join print ;
TUPLE: Person { Name string read-only } { Email string read-only } { Age integer read-only } ; : person-with-name ( person name -- person' ) swap [ Email>> ] [ Age>> ] bi Person boa ; : person-with-email ( person email -- person' ) swap [ Name>> ] [ Age>> ] bi swapd Person boa ; : person-with-age ( person age -- person' ) swap [ Name>> ] [ Email>> ] bi rot Person boa ; : demo ( -- person ) "Scott" "s@example.com" 21 Person boa "Tom" person-with-name "tom@example.com" person-with-email 42 person-with-age ;
: >roman-numerals ( n -- str ) CHAR: I <string> "IIIII" "V" replace "VV" "X" replace "XXXXX" "L" replace "LL" "C" replace ! special cases: "VIIII" "IX" replace "IIII" "IV" replace "LXXXX" "XC" replace "XXXX" "XL" replace ; : >roman-numerals-alt ( n -- str ) CHAR: I <string> { { "IIIII" "V" } { "VV" "X" } { "XXXXX" "L" } { "LL" "C" } ! special cases: { "VIIII" "IX" } { "IIII" "IV" } { "LXXXX" "XC" } { "XXXX" "XL" } } [ first2 replace ] each ;
Nice! I’ve been here only for a week or two and I’ve seen Factor mentioned 2 times. I didn’t know Factor was such a factor still. BTW… look at my username :)
Glancing back at this, the following:
Should probably instead be:
: pipeline ( seq -- n ) [ 2 + ] map [ 3 > ] count ;
It could be shorter but that wouldn’t demonstrate the pipelining the same way.
When this was posted on another site, I translated the code examples from the slides into Factor. So . . . here they are!
This time I’m leaving out the import statements, for brevity. All the line breaks and indentations are optional.
Nice! I’ve been here only for a week or two and I’ve seen Factor mentioned 2 times. I didn’t know Factor was such a factor still. BTW… look at my username :)
Glancing back at this, the following:
Should probably instead be:
It could be shorter but that wouldn’t demonstrate the pipelining the same way.