Skip to content

Instantly share code, notes, and snippets.

@willmurphyscode
Created January 31, 2018 11:30
Show Gist options
  • Save willmurphyscode/71074de7a8f544ddd27ebec4b3f287dd to your computer and use it in GitHub Desktop.
Save willmurphyscode/71074de7a8f544ddd27ebec4b3f287dd to your computer and use it in GitHub Desktop.
fn reduce_subtraction(stack: &mut Vec<Token>) -> Result<Token, RuntimeError> {
let operands = unwrap_operand_tokens(stack);
match operands {
Ok(mut operand_vec) =>{
let initial_positive_option = operand_vec.pop();
if let Some(initial_positive) = initial_positive_option {
Ok(Token::Operand(
operand_vec
.iter()
.fold(initial_positive, |sum, value| sum - value)))
} else {
Err(RuntimeError{})
}
},
Err(_) => Err(RuntimeError{})
}
}
fn reduce_division(stack: &mut Vec<Token>) -> Result<Token, RuntimeError> {
let operands = unwrap_operand_tokens(stack);
match operands {
Ok(mut operand_vec) =>{
let initial_numerator_option = operand_vec.pop();
if let Some(initial_numerator) = initial_numerator_option {
Ok(Token::Operand(
operand_vec
.iter()
.fold(initial_numerator, |numerator, value| numerator / value)))
} else {
Err(RuntimeError{})
}
},
Err(_) => Err(RuntimeError{})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment