Skip to content

Commit

Permalink
Slightly clearer
Browse files Browse the repository at this point in the history
  • Loading branch information
lpil committed Jan 16, 2024
1 parent 0ac556f commit c6b4989
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions exercises/practice/wordy/.meta/example.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -45,28 +45,28 @@ fn tokenize(question: String) -> Result(List(Token), Error) {
}

fn evaluate(tokens: List(Token)) -> Result(Int, Error) {
use [Operand(result)] <- result.then(
list.try_fold(tokens, [], fn(
previous_tokens: List(Token),
current_token: Token,
) {
case previous_tokens, current_token {
[], Operand(_) -> Ok([current_token])

[Operand(_)], Operator(_) -> Ok([current_token, ..previous_tokens])

[Operator(operation), Operand(left_operand)], Operand(right_operand) ->
case operation(left_operand, right_operand) {
Ok(accumulated_result) -> Ok([Operand(accumulated_result)])
Error(Nil) -> Error(ImpossibleOperation)
}

_, _ -> Error(SyntaxError)
}
}),
)

Ok(result)
list.try_fold(tokens, [], fn(
previous_tokens: List(Token),
current_token: Token,
) {
case previous_tokens, current_token {
[], Operand(_) -> Ok([current_token])

[Operand(_)], Operator(_) -> Ok([current_token, ..previous_tokens])

[Operator(operation), Operand(left_operand)], Operand(right_operand) ->
case operation(left_operand, right_operand) {
Ok(accumulated_result) -> Ok([Operand(accumulated_result)])
Error(Nil) -> Error(ImpossibleOperation)
}

_, _ -> Error(SyntaxError)
}
})
|> result.map(fn(result) {
let assert [Operand(result)] = result
result
})
}

fn parse_tokens(chunks: List(String)) -> Result(List(Token), Error) {
Expand Down

0 comments on commit c6b4989

Please sign in to comment.