feat: add method max to Outcome
This commit is contained in:
@@ -0,0 +1,7 @@
|
|||||||
|
use oxidice_lib::dice::*;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let roll_result = Handful::grab(2, 20).roll();
|
||||||
|
let result = roll_result.clone().max().unwrap();
|
||||||
|
println!("{} -> {}", roll_result, result);
|
||||||
|
}
|
||||||
@@ -2,4 +2,5 @@ pub mod die_new;
|
|||||||
pub mod die_roll;
|
pub mod die_roll;
|
||||||
pub mod handful_grab;
|
pub mod handful_grab;
|
||||||
pub mod handful_roll;
|
pub mod handful_roll;
|
||||||
|
pub mod outcome_max;
|
||||||
pub mod outcome_sum;
|
pub mod outcome_sum;
|
||||||
|
|||||||
41
01.workspace/oxidice_lib/src/imp/outcome_max.rs
Normal file
41
01.workspace/oxidice_lib/src/imp/outcome_max.rs
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
use crate::*;
|
||||||
|
|
||||||
|
fn max_of(values: Vec<u16>) -> Result<Outcome, FailedTo> {
|
||||||
|
if values.is_empty() {
|
||||||
|
return Err(FailedTo::ProcessInput);
|
||||||
|
}
|
||||||
|
let ret = Ok(Outcome::Scalar(u16::MAX));
|
||||||
|
ret.and_then(|_| {
|
||||||
|
values
|
||||||
|
.iter()
|
||||||
|
.max()
|
||||||
|
.ok_or(FailedTo::FindMin)
|
||||||
|
.map(|min| Outcome::Scalar(*min))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Outcome {
|
||||||
|
pub fn max(self) -> Result<Outcome, FailedTo> {
|
||||||
|
match self {
|
||||||
|
Outcome::Scalar(value) => Ok(Outcome::Scalar(value)),
|
||||||
|
Outcome::List(values) => max_of(values),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod unit_tests {
|
||||||
|
use super::*;
|
||||||
|
#[test]
|
||||||
|
fn check_max() {
|
||||||
|
let outcome = Handful::grab(2, 20).roll();
|
||||||
|
let max = outcome.clone().max().unwrap();
|
||||||
|
match outcome {
|
||||||
|
Outcome::List(values) => {
|
||||||
|
let expected_max = values.iter().max().unwrap();
|
||||||
|
assert_eq!(max, Outcome::Scalar(*expected_max));
|
||||||
|
}
|
||||||
|
_ => panic!("outcome is not a list"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
#[derive(Debug, PartialEq, PartialOrd, Eq, Ord, Clone, Copy, Hash)]
|
#[derive(Debug, PartialEq, PartialOrd, Eq, Ord, Clone, Copy, Hash)]
|
||||||
pub enum E {
|
pub enum E {
|
||||||
|
FindMin,
|
||||||
ProcessInput,
|
ProcessInput,
|
||||||
SumValues,
|
SumValues,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user