Skip to content

Instantly share code, notes, and snippets.

@sathish316

sathish316/oneplus.scala

Last active Sep 20, 2015
Embed
What would you like to do?
Generate email ids for oneplus registration based on an amazon review (http://www.amazon.in/review/REA7WFX7PV3KO)
object OnePlus {
def main(args: Array[String]) {
generate_aliases("typeyouremailhere@gmail.com").foreach(println)
}
def generate_aliases(email:String) = {
val email_pattern = """(\w+)@([\w\.]+)""".r
email match {
case email_pattern(address, domain) => {
interleave_all_with(address, '.').map {case address => s"$address@$domain"}
}
}
}
private def interleave_all_with(address:String, char:Char):Array[String] = interleave_all(address, char, address.length-1)
private def interleave_all(address:String, char:Char, times:Int):Array[String] = times match {
case 1 => interleave(address, char, 1, address.length, Array.empty[String])
case n => {
val aliases = interleave_all(address, char, times-1)
val results = aliases ++ aliases.flatMap {case (alias:String) => interleave_with(alias, char)}
results.map(result => result.replaceAll("\\.\\.", ".")).filterNot(_.endsWith(".")).distinct
}
}
private def interleave_with(address:String, char: Char):Array[String] = interleave(address, char, 1, address.length, Array.empty[String])
private def interleave(address:String, char: Char, from: Int, to: Int, results: Array[String]): Array[String] = if (from < to)
interleave(address, char, from + 1, to, results ++ Array(alias(address, char, from, to)))
else
results
private def alias(address:String, char:Char, from:Int, to:Int) = address.slice(0, from) + char + address.slice(from, to)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment