diff --git a/src/main/kotlin/CustomAdt.kt b/src/main/kotlin/CustomAdt.kt index c30e0c4..5c09f41 100644 --- a/src/main/kotlin/CustomAdt.kt +++ b/src/main/kotlin/CustomAdt.kt @@ -8,8 +8,20 @@ sealed interface ParseResult { object CustomAdt { - fun parseInt(str: String): ParseResult { - TODO() + fun parseInt(input: String): ParseResult { + val (sign, elementsToSkip) = if (input.startsWith("-")) -1 to 1 else 1 to 0 + + return input.withIndex() + .drop(elementsToSkip) + .map { (index, char) -> + if (char in '0'..'9') { + char - '0' + } else { + return ParseResult.Failure(index, char) + } + } + .fold(0) { acc, digit -> acc * 10 + digit } + .let { result -> ParseResult.Success(result * sign) } } } \ No newline at end of file