1use core::{fmt, ops};
2
3use crate::{
4 impl_computation_fn_for_unary, impl_core_ops,
5 peano::{Suc, Zero},
6 Computation, ComputationFn, NamedArgs,
7};
8
9#[derive(Clone, Copy, Debug)]
10pub struct Sum<A>(pub A)
11where
12 Self: Computation;
13
14impl<A, D> Computation for Sum<A>
15where
16 A: Computation<Dim = Suc<D>>,
17 A::Item: ops::Add,
18{
19 type Dim = Zero;
20 type Item = <A::Item as ops::Add>::Output;
21}
22
23impl_computation_fn_for_unary!(Sum);
24
25impl_core_ops!(Sum<A>);
26
27impl<A> fmt::Display for Sum<A>
28where
29 Self: Computation,
30 A: fmt::Display,
31{
32 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33 write!(f, "{}.sum()", self.0)
34 }
35}
36
37#[cfg(test)]
38mod tests {
39 use proptest::prelude::*;
40 use test_strategy::proptest;
41
42 use crate::{val1, Computation};
43
44 #[proptest]
45 fn sum_should_display(xs: Vec<i32>) {
46 prop_assert_eq!(
47 val1!(xs.clone()).sum().to_string(),
48 format!("{}.sum()", val1!(xs))
49 );
50 }
51}