Paste: Better fizzbuzz

Author: Dan Lucraft
Mode: factor
Date: Wed, 30 Sep 2009 20:56:45
Plain Text |
USING: math.ranges math sequences prettyprint kernel ;
IN: fizz-buzz;

: divides? ( x y -- x bool ) dupd mod 0 = ;

: output ( x bool bool -- x y )   
    [ [ "FizzBuzz" ] [ "Fizz" ] if ] 
    [ [ "Buzz" ] [ dup ] if ] 
    if ;

: fizz-buzz ( start end -- )
    [a,b] [ 5 divides? swap 3 divides? swapd output . drop ] each ;
    
1 10 fizz-buzz

Annotation: w/o swapping

Author: me
Mode: factor
Date: Thu, 17 Dec 2009 19:09:07
Plain Text |
: divides ( x y -- ? ) mod zero? ;

: divides-5-or-3 ( x -- ? ? ) 
   [ 5 divides? ] [ 3 divides? ] bi ;

: output ( n ? ? ? -- )
  [ "Fizz" write ] when
  [ "Buzz" write ] when
  [ drop ] [ number>string write ] if
  "\n" write ;

: fizzbuzz ( -- ) 
  100 [1,b] [ dup divides-5-or-3 [ or ] 2keep output ] each ;

New Annotation

Summary:
Author:
Mode:
Body: