Rot13 in Groovy (refactored)





3
Date Submitted Thu. Nov. 15th, 2007 2:37 PM
Revision 1 of 1
Helper BerndSchiffer
Tags groovy
Comments 1 comments
Answer to a groovier solution to Hamlet D'Arcy's Rot13 algorithm. This is the refactored version with less redundance.

def redundanceFreeRot13 = {
  it.inject(''){ result, c ->
    result + (char)((c as char) + rotCount(c))
  }
}

def rotCount(c) {
  if(c in upperAndLowerCase('a'..'m')) return  13
  if(c in upperAndLowerCase('n'..'z')) return -13
  0
}

def upperAndLowerCase(characters) {
  characters*.toUpperCase() + characters*.toLowerCase()
}
 

Comments

Comments Unrefactored Version
Thu. Nov. 15th, 2007 2:43 PM    Helper BerndSchiffer

Voting

Votes Down