• @AndyOPM
    link
    18 months ago

    Examples from the slides, but in Factor:

    USING: kernel math ;
    IN: pipeline-demo.calc
    
    : 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
    ;
    
    USING: math sequences sequences.extras ;
    IN: pipeline-demo.collection
    
    : pipeline ( seq -- n )
      [ 2 + ]
      [ 3 > ] map-filter
      length
    ;
    
    USING: io kernel math.functions math.parser ranges sequences ;
    IN: pipeline-demo.fizzbuzz
    
    : (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
    ;
    
    USING: accessors kernel math strings ;
    IN: pipeline-demo.person
    
    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" "[email protected]" 21 Person boa
      "Tom" person-with-name
      "[email protected]" person-with-email
      42 person-with-age
    ;
    
    USING: sequences splitting strings ;
    IN: pipeline-demo.roman
    
    : >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
    ;