]> gitweb.factorcode.org Git - factor.git/blobdiff - extra/rot13/rot13.factor
Initial import
[factor.git] / extra / rot13 / rot13.factor
diff --git a/extra/rot13/rot13.factor b/extra/rot13/rot13.factor
new file mode 100644 (file)
index 0000000..9849f4e
--- /dev/null
@@ -0,0 +1,24 @@
+! Copyright (C) 2006 Daniel Ehrenberg
+! See http://factorcode.org/license.txt for BSD license.
+USING: kernel math sequences strings io combinators ;
+IN: rot13
+
+: 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 ;
+
+: rot13-demo ( -- )
+    "Please enter a string:" print flush
+    readln [
+        "Your string: " write dup print
+        "Rot13:       " write rot13 print
+    ] when* ;
+
+MAIN: rot13-demo