Skip to content

Instantly share code, notes, and snippets.

@MinCha
Created February 10, 2015 12:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MinCha/badc208ef01fd216b278 to your computer and use it in GitHub Desktop.
Save MinCha/badc208ef01fd216b278 to your computer and use it in GitHub Desktop.
My Functional Class #1
case class AddressString(address: Address, newAddress: NewAddress) {
private def getNTh(n: Int, f: List[String] => Boolean): String = {
def getOnlyLastThree(string: String): List[String] = {
if (string.split(" ").length > 3)
string.split(" ").toList.tail
else
string.split(" ").toList
}
val a = if (f(getOnlyLastThree(address.region))) getOnlyLastThree(address.region)(n) else ""
val b = if (f(getOnlyLastThree(newAddress.toString))) getOnlyLastThree(newAddress.toString)(n) else ""
if (a == b) a else a + " " + b
}
def getFirst = {
getNTh(0, _.size == 3)
}
def getSecond = {
getNTh(1, _.size >= 2)
}
def getThird = {
getNTh(2, _.size >= 1)
}
def toTuple = (address.region, newAddress.toString)
def isExisting: Boolean = address.region != "" && newAddress.region != ""
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment