Skip to content

Instantly share code, notes, and snippets.

@ymnk
Created September 21, 2010 11:32
Show Gist options
  • Save ymnk/589543 to your computer and use it in GitHub Desktop.
Save ymnk/589543 to your computer and use it in GitHub Desktop.
$ scala
Welcome to Scala version 2.8.0.final (Java HotSpot(TM) Client VM, Java 1.6.0_16).
Type in expressions to have them evaluated.
Type :help for more information.
scala> def caesarCipher(text:String, n: Int) = {
| val l = (' ' to '~').toList
| val ll = (l drop n) ::: (l take n)
| text map (l compose ll.zipWithIndex.toMap) mkString
| }
caesarCipher: (text: String,n: Int)String
scala> caesarCipher("The quick brown fox jumps over the lazy dog", 3)
res0: String = Qeb|nrf`h|_oltk|clu|grjmp|lsbo|qeb|i^wv|ald
scala> caesarCipher("Qeb|nrf`h|_oltk|clu|grjmp|lsbo|qeb|i^wv|ald", (' ' to '~').toList.size-3)
res1: String = The quick brown fox jumps over the lazy dog
scala>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment