Created
February 10, 2015 12:53
-
-
Save MinCha/badc208ef01fd216b278 to your computer and use it in GitHub Desktop.
My Functional Class #1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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