Skip to content

Commit 5873001

Browse files
committed
refactor: simplify operator handling with guard clauses
1 parent de8d68c commit 5873001

1 file changed

Lines changed: 29 additions & 39 deletions

File tree

examples/calculator/calc.mbt

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -184,33 +184,27 @@ fn handle_clear_entry(state : CalculatorState) -> CalculatorState {
184184
/// Stores selected operator and computes chained operations.
185185
fn handle_operator(state : CalculatorState, op : String) -> CalculatorState {
186186
let state = if state.has_error { clear_all_state() } else { state }
187-
match parse_number(state.current_input) {
188-
Some(current_value) => {
189-
let (accumulator, current_input) = if !state.has_accumulator {
190-
(current_value, state.current_input)
191-
} else if state.pending_operator != "" && !state.should_reset_input {
192-
match
193-
apply_operator(
194-
state.accumulator,
195-
current_value,
196-
state.pending_operator,
197-
) {
198-
Some(result) => (result, format_number(result))
199-
None => return show_error_state()
200-
}
201-
} else {
202-
(state.accumulator, state.current_input)
203-
}
204-
{
205-
current_input,
206-
accumulator,
207-
has_accumulator: true,
208-
pending_operator: op,
209-
should_reset_input: true,
210-
has_error: false,
211-
}
187+
guard parse_number(state.current_input) is Some(current_value) else {
188+
return show_error_state()
189+
}
190+
let (accumulator, current_input) = if !state.has_accumulator {
191+
(current_value, state.current_input)
192+
} else if state.pending_operator != "" && !state.should_reset_input {
193+
match
194+
apply_operator(state.accumulator, current_value, state.pending_operator) {
195+
Some(result) => (result, format_number(result))
196+
None => return show_error_state()
212197
}
213-
None => show_error_state()
198+
} else {
199+
(state.accumulator, state.current_input)
200+
}
201+
{
202+
current_input,
203+
accumulator,
204+
has_accumulator: true,
205+
pending_operator: op,
206+
should_reset_input: true,
207+
has_error: false,
214208
}
215209
}
216210

@@ -223,19 +217,15 @@ fn handle_equal(state : CalculatorState) -> CalculatorState {
223217
if !state.has_accumulator || state.pending_operator == "" {
224218
return state
225219
}
226-
match parse_number(state.current_input) {
227-
Some(current_value) =>
228-
match
229-
apply_operator(state.accumulator, current_value, state.pending_operator) {
230-
Some(result) =>
231-
{
232-
..clear_all_state(),
233-
current_input: format_number(result),
234-
should_reset_input: true,
235-
}
236-
None => show_error_state()
237-
}
238-
None => show_error_state()
220+
guard parse_number(state.current_input) is Some(current_value) &&
221+
apply_operator(state.accumulator, current_value, state.pending_operator)
222+
is Some(result) else {
223+
return show_error_state()
224+
}
225+
{
226+
..clear_all_state(),
227+
current_input: format_number(result),
228+
should_reset_input: true,
239229
}
240230
}
241231

0 commit comments

Comments
 (0)