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
//! Provides the ability to fold tuples.
//!
//! The documentation page of [`Folder`] has many examples available.
use crate::;
/// Define folders that fold an element of the tuple and specify another folder to be used to
/// fold the next element of the tuple.
///
/// To fold a tuple with type [`Tuple<T0, T1, ... Tn>`](crate::Tuple), you need to construct a custom folder type,
/// which implements [`Folder<T0, Acc>`], and its [`NextFolder`](Folder::NextFolder)
/// implements [`Folder<T1, <F as Folder<T0, Acc>>::Output>`](Folder), and so on.
/// Pass your folder to tuple's [`fold()`](TupleLike::fold()) method, then the tuple will call
/// folder's [`fold()`](Folder::fold()) method and move its first element in, and then the tuple
/// move its second element in its [`NextFolder`](Folder::NextFolder)'s [`fold()`](Folder::fold()) method,
/// and so on.
///
/// NOTE: Folding a tuple will consume it. If this is not what you want, call [`as_ref()`](TupleLike::as_ref())
/// or [`as_mut()`](TupleLike::as_mut()) to create a new tuple that references its all members before folding.
///
/// # Quickly build a folder by macros
///
/// Here are two ways you can quickly build a folder.
///
/// ## Fold tuples by element types
///
/// The [`folder!`](crate::folder!) macro helps you build a folder that folds tuples according to their element types.
///
/// For example:
///
/// ```
/// use tuplez::{folder, tuple, TupleLike};
///
/// let tup = tuple!(Some(1), "2", Some(3.0));
/// let result = tup.fold(
/// folder! { String; // Type of `acc` of all closures must be the same and annotated at the front
/// |acc, x: &str| { acc + &x.to_string() }
/// <T: ToString> |acc, x: Option<T>| { acc + &x.unwrap().to_string() }
/// },
/// String::new(),
/// );
/// assert_eq!(result, "123".to_string());
/// ```
///
/// ## Fold tuples in order of their elements
///
/// You can create a new tuple with the same number of elements, whose elements are all callable objects that accepts the accumulation value
/// and an element and returns new accumulation value ([`FnOnce(Acc, T) -> Acc`](std::ops::FnOnce)), then, you can use that tuple as a folder.
///
/// For example:
///
/// ```
/// use tuplez::{tuple, TupleLike};
///
/// let tup = tuple!(1, "2", 3.0);
/// let result = tup.fold(
/// tuple!(
/// |acc, x| (acc + x) as f64,
/// |acc: f64, x: &str| acc.to_string() + x,
/// |acc: String, x| acc.parse::<i32>().unwrap() + x as i32,
/// ), // Type of `acc` of each closure is the return type of the previous closure.
/// 0,
/// );
/// assert_eq!(result, 15);
/// ```
///
/// # Custom folder
///
/// If you are not satisfied with the above three methods, you can customize a folder.
///
/// Here is an example, very simple but sufficient to show how to use:
///
/// ```
/// use std::collections::VecDeque;
/// use tuplez::{fold::Folder, tuple, TupleLike};
///
/// struct MyFolder(VecDeque<i32>);
///
/// impl<T: std::fmt::Display> Folder<T, String> for MyFolder {
/// type Output = String;
/// type NextFolder = Self;
///
/// fn fold(mut self, acc: String, value: T) -> (Self::Output, Self::NextFolder) {
/// (
/// if self.0.len() == 1 {
/// acc + &format!("{}[{}]", value, self.0.pop_front().unwrap())
/// } else {
/// acc + &format!("{}[{}],", value, self.0.pop_front().unwrap())
/// },
/// self,
/// )
/// }
/// }
///
/// let tup = tuple!(3.14, "hello", 25);
/// let result = tup.fold(MyFolder(VecDeque::from(vec![1, 2, 3])), String::new());
/// assert_eq!(result, "3.14[1],hello[2],25[3]");
/// ```
/// Fold the tuple.
///
/// # The folder `F`
///
/// For folding [`Tuple<T0, T1, ... Tn>`](crate::Tuple), you need to build a folder,
/// which needs to implement [`Folder<T0, Acc>`], and the [`NextFolder`](Folder::NextFolder)
/// needs to implement [`Folder<T1, <F as Folder<T0, Acc>>::Output>`](Folder), and so on.
///
/// See the documentation page of [`Folder`] for details.