Last active
February 5, 2018 08:37
-
-
Save jmfayard/05196271298ac067272b6c26a577a00b to your computer and use it in GitHub Desktop.
This file contains 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
// https://medium.com/@garnop/safe-concise-text-parsing-with-regex-destructuring-in-kotlin-b8f77ef1e30c | |
fun parsePhoneNumber(input: String): PhoneNumber { | |
val invalids = input.filterNot { it in '0'..'9' || it == '-' } | |
require(invalids.isEmpty()) { "Input $input has invalid characters : $invalids" } | |
val (areaCode, prefix, lineNumber) = input.split("-").map(String::toInt) | |
return PhoneNumber(areaCode, prefix, lineNumber) | |
} | |
fun main(args: Array<String>) { | |
val input = | |
require(parsePhoneNumber("888-888-8888") == PhoneNumber(888, 888, 8888)) | |
} | |
data class PhoneNumber( | |
val areaCode: Int, | |
val prefix: Int, | |
val lineNumber: Int | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment