haar_lib/algebra/
first_last.rs1pub use crate::algebra::traits::*;
3use crate::impl_algebra;
4
5#[derive(Clone, Copy, Default, Debug, PartialEq, Eq)]
7pub struct First<T>(pub Option<T>);
8#[derive(Clone, Copy, Default, Debug, PartialEq, Eq)]
10pub struct Last<T>(pub Option<T>);
11
12impl_algebra!(
13 [T]; First<T>;
14 op: |a: Self, b| match a.0 {
15 Some(_) => a,
16 None => b
17 };
18 id: Self(None);
19 assoc;
20 idem;
21);
22impl_algebra!(
23 [T]; Last<T>;
24 op: |a, b: Self| match b.0 {
25 Some(_) => b,
26 None => a
27 };
28 id: Self(None);
29 assoc;
30 idem;
31);
32
33#[cfg(test)]
34mod tests {
35 use super::*;
36
37 #[test]
38 fn test() {
39 let a = [None, None, Some(1), None, Some(3), Some(5)];
40
41 let b: Vec<_> = a.into_iter().map(First).collect();
42 dbg!(b.iter().fold(First::id(), |x, y| First::op(x, *y)));
43
44 let b: Vec<_> = a.into_iter().map(Last).collect();
45 dbg!(b.iter().fold(Last::id(), |x, y| Last::op(x, *y)));
46 }
47}