owasm_kit/ext/
mod.rs

1//! # Owasm Standard Library
2use crate::oei;
3
4pub mod cmp;
5pub mod stats;
6
7/// Returns an iterator of raw reports for the given external ID with nonzero status.
8pub fn load_input_raw(eid: i64) -> impl Iterator<Item = String> {
9    (0..oei::get_ask_count()).filter_map(move |idx| oei::get_external_data(eid, idx).ok())
10}
11
12/// Returns an iterator of raw data points for the given external ID, parsed into
13/// the parameterized type using `std::str::FromStr` trait. Skip data points
14/// with nonzero status OR cannot be parsed.
15pub fn load_input<T>(eid: i64) -> impl Iterator<Item = T>
16where
17    T: std::str::FromStr,
18{
19    load_input_raw(eid).filter_map(|e| e.trim_end().parse::<T>().ok())
20}
21
22/// Returns the average value of the given external ID, ignoring unsuccessful reports.
23pub fn load_average<T>(eid: i64) -> Option<T>
24where
25    T: std::str::FromStr + num::Num,
26{
27    stats::average(load_input(eid).collect())
28}
29
30/// Returns the median value of the given external ID, ignoring unsuccessful reports.
31pub fn load_median_integer<T>(eid: i64) -> Option<T>
32where
33    T: std::str::FromStr + std::cmp::Ord + num::Num + num::NumCast,
34{
35    stats::median_integer(load_input(eid).collect())
36}
37
38/// Returns the median value of the given external ID, ignoring unsuccessful reports.
39pub fn load_median_float<T>(eid: i64) -> Option<T>
40where
41    T: std::str::FromStr + num::Float + num::NumCast,
42{
43    stats::median_float(load_input(eid).collect())
44}
45
46/// Returns the majority value of the given external ID, ignoring unsuccessful reports.
47pub fn load_majority<T>(eid: i64) -> Option<T>
48where
49    T: std::str::FromStr + std::cmp::PartialEq,
50{
51    stats::majority(load_input(eid).collect())
52}