summaryrefslogtreecommitdiff
path: root/src/library/math.rs
blob: 89c45ecc36ef74206e9483c441e7fa05610f9d2f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use std::cmp::Ordering;

use super::*;

/// `min`: The minimum of two values.
///
/// # Positional parameters
/// - Values: variadic, must be comparable.
///
/// # Return value
/// The minimum of the sequence of values. For equal elements, the first one is
/// returned.
pub fn min(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
    minmax(ctx, args, Ordering::Less)
}

/// `max`: The maximum of two values.
///
/// # Positional parameters
/// - Values: variadic, must be comparable.
///
/// # Return value
/// The maximum of the sequence of values. For equal elements, the first one is
/// returned.
pub fn max(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
    minmax(ctx, args, Ordering::Greater)
}

/// Find the minimum or maximum of a sequence of values.
fn minmax(ctx: &mut EvalContext, args: &mut FuncArgs, goal: Ordering) -> Value {
    let mut extremum = None;

    while let Some(value) = args.eat::<Value>(ctx) {
        if let Some(prev) = &extremum {
            match value.cmp(&prev) {
                Some(ordering) if ordering == goal => extremum = Some(value),
                Some(_) => {}
                None => {
                    ctx.diag(error!(
                        args.span,
                        "cannot compare {} with {}",
                        prev.type_name(),
                        value.type_name(),
                    ));
                    return Value::Error;
                }
            }
        } else {
            extremum = Some(value);
        }
    }

    extremum.unwrap_or_else(|| {
        args.eat_expect::<Value>(ctx, "value");
        Value::Error
    })
}