Factor Language Blog

rot13 in Factor

Thursday, November 30, 2006

Daniel Ehrenberg implemented the rot13 algorithm in Factor. I added it to the demos/ directory; it is very simple and elegant:

IN: rot13
USING: kernel math sequences strings ;

: rotate ( ch base -- ch ) tuck - 13 + 26 mod + ;

: rot-letter ( ch -- ch )
    {
        { [ dup letter? ] [ CHAR: a rotate ] }
        { [ dup LETTER? ] [ CHAR: A rotate ] }
        { [ t ] [ ] }
    } cond ;

: rot13 ( string -- string ) [ rot-letter ] map ;