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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use crate::{Generator, ValueResult};
use core::num::Wrapping;
pub trait Sum<A = Self>: Sized {
fn sum<G>(gen: G) -> Self
where
G: Generator<Output = A>;
}
pub trait Product<A = Self>: Sized {
fn product<G>(gen: G) -> Self
where
G: Generator<Output = A>;
}
macro_rules! integer_sum_product {
(@impls $zero:expr, $one:expr, $($a:ty)*) => ($(
impl Sum for $a {
#[inline]
fn sum<G: Generator<Output=Self>>(mut gen: G) -> Self {
let mut ret = $zero;
gen.run(
|x| {
ret += x;
ValueResult::MoreValues
}
);
ret
}
}
impl<'a> Sum<&'a $a> for $a {
#[inline]
fn sum<G: Generator<Output=&'a Self>>(mut gen: G) -> Self {
let mut ret = $zero;
gen.run(
|x| {
ret += x;
ValueResult::MoreValues
}
);
ret
}
}
impl Product for $a {
#[inline]
fn product<G: Generator<Output=Self>>(mut gen: G) -> Self {
let mut ret = $one;
gen.run(|x| {
ret *= x;
ValueResult::MoreValues
}
);
ret
}
}
impl<'a> Product<&'a $a> for $a {
#[inline]
fn product<G: Generator<Output=&'a Self>>(mut gen: G) -> Self {
let mut ret = $one;
gen.run(|x| {
ret *= x;
ValueResult::MoreValues
}
);
ret
}
}
)*);
($($a:ty)*) => (
integer_sum_product!(@impls 0, 1,
$($a)*);
integer_sum_product!(@impls Wrapping(0), Wrapping(1),
$(Wrapping<$a>)*);
);
}
macro_rules! float_sum_product {
($($a:ty)*) => ($(
impl Sum for $a {
#[inline]
fn sum<G: Generator<Output=Self>>(mut gen: G) -> Self {
let mut ret = 0.0;
gen.run(
|x| {
ret += x;
ValueResult::MoreValues
}
);
ret
}
}
impl<'a> Sum<&'a $a> for $a {
#[inline]
fn sum<G: Generator<Output=&'a Self>>(mut gen: G) -> Self {
let mut ret = 0.0;
gen.run(
|x| {
ret += x;
ValueResult::MoreValues
}
);
ret
}
}
impl Product for $a {
#[inline]
fn product<G: Generator<Output=Self>>(mut gen: G) -> Self {
let mut ret = 1.0;
gen.run(|x| {
ret *= x;
ValueResult::MoreValues
}
);
ret
}
}
impl<'a> Product<&'a $a> for $a {
#[inline]
fn product<G: Generator<Output=&'a Self>>(mut gen: G) -> Self {
let mut ret = 1.0;
gen.run(|x| {
ret *= x;
ValueResult::MoreValues
}
);
ret
}
})*)
}
integer_sum_product! { i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize }
float_sum_product! { f32 f64 }
#[cfg(test)]
mod tests {
use super::*;
use crate::{GeneratorExt, IntoGenerator};
#[test]
fn sum() {
let data = [1, 2, 3, 4];
assert_eq!(i32::sum(data.into_gen().copied()), 10);
assert_eq!(i32::sum(data.into_gen()), 10);
let data = [Wrapping(u32::MAX), Wrapping(1)];
assert_eq!(Wrapping::<u32>::sum(data.into_gen()), Wrapping(0));
}
#[test]
fn product() {
let data = [2, 3, 4];
let expected = 2 * 3 * 4;
assert_eq!(i32::product(data.into_gen().copied()), expected);
assert_eq!(i32::product(data.into_gen()), expected);
}
}