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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
//! Functions and types to do something special when repeating for the first or
//! last time (or in between!). This crate offers two distinct features:
//!
//! - [`IterStatusExt::with_status`]: a new method for **iterators**, that
//!   creates a new iterator which yields the item paired with information to
//!   tell you if this is the first/last item.
//! - [`SkipFirst`]: a simple struct to help you always do something, except on
//!   the first repetition. Works without iterators, too!

use std::{
    iter::{FusedIterator, Peekable},
};

/// Allows you to always do something, except the first time.
///
/// Internally, this is simply a `bool`. It stores whether
/// [`skip_first`][SkipFirst::skip_first] has already been called. This struct
/// is really just a wrapper for a dead simple logic you could easily write
/// yourself. However, if you need to write it multiple times, it's better to
/// use this type to avoid duplicate code.
///
/// # Example
///
/// In this example, it's also possible to use [`IterStatusExt::with_status`].
///
/// ```
/// use splop::SkipFirst;
///
/// let mut comma = SkipFirst::new();
/// for name in &["peter", "ingrid", "barbara"] {
///     comma.skip_first(|| print!(", "));
///     print!("{}", name);
/// }
/// println!();
///
/// // Printed "peter, ingrid, barbara"
/// ```
pub struct SkipFirst {
    first: bool,
}

impl SkipFirst {
    /// Creates a new instance of `SkipFirst`.
    pub fn new() -> Self {
        Self {
            first: true,
        }
    }

    /// Executes the given function, except the first time this method is
    /// called on this instance.
    ///
    /// # Example
    ///
    /// ```
    /// use splop::SkipFirst;
    ///
    /// let mut v = Vec::new();
    /// let mut skipper = SkipFirst::new();
    /// skipper.skip_first(|| v.push(1));  // won't be executed
    /// skipper.skip_first(|| v.push(2));  // will be executed
    /// skipper.skip_first(|| v.push(3));  // will be executed
    ///
    /// assert_eq!(v, [2, 3]);
    /// ```
    ///
    /// Note that the state "has been called already" is stored in the
    /// [`SkipFirst`] instance and not globally:
    ///
    /// ```
    /// use splop::SkipFirst;
    ///
    /// let mut v = Vec::new();
    /// let mut skipper_a = SkipFirst::new();
    /// let mut skipper_b = SkipFirst::new();
    /// skipper_a.skip_first(|| v.push("a"));  // won't be executed
    /// skipper_b.skip_first(|| v.push("b"));  // won't be executed
    /// skipper_b.skip_first(|| v.push("b2"));  // will be executed
    /// skipper_a.skip_first(|| v.push("a2"));  // will be executed
    ///
    /// assert_eq!(v, ["b2", "a2"]);
    /// ```
    pub fn skip_first<R>(&mut self, f: impl FnOnce() -> R) -> Option<R> {
        if self.first {
            self.first = false;
            None
        } else {
            Some(f())
        }
    }
}

/// Iterator wrapper which keeps track of the status. See
/// [`IterStatusExt::with_status`] for more information.
pub struct WithStatus<I: Iterator> {
    iter: Peekable<I>,
    first: bool,
}

impl<I: Iterator> WithStatus<I> {
    fn new(iter: I) -> Self {
        Self {
            iter: iter.peekable(),
            first: true,
        }
    }
}

impl<I: Iterator> Iterator for WithStatus<I> {
    type Item = (I::Item, Status);

    fn next(&mut self) -> Option<Self::Item> {
        // Get the next item from the iterator.
        let item = self.iter.next();

        let status = Status {
            first: self.first,
            // Since we already got the real item above, we can now peek if
            // there is still another item.
            last: self.iter.peek().is_none(),
        };

        if self.first {
            self.first = false;
        }

        item.map(|elem| (elem, status))
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        // We pass through the `size_hint` method, as the underlying iterator
        // might have size information.
        self.iter.size_hint()
    }
}

// Implement traits when the underlying iterator implements them.
impl<I: FusedIterator> FusedIterator for WithStatus<I> {}
impl<I: ExactSizeIterator> ExactSizeIterator for WithStatus<I> {
    fn len(&self) -> usize {
        self.iter.len()
    }
}

/// Adds the `with_status` method to all iterators.
pub trait IterStatusExt: Iterator + Sized {
    /// Creates an iterator that yields the original items paired with a
    /// status, which tells you if the item is the first and/or last one.
    ///
    /// The new iterator's item has the type `(Self::Item, Status)`. See
    /// [`Status`] for detailed information. The new iterator uses `peekable()`
    /// internally, so if the `next()` call of the underlying iterator has
    /// side effects, those will be visible earlier than expected.
    ///
    /// # Example
    ///
    /// ```
    /// use splop::IterStatusExt;
    ///
    ///
    /// let mut s = String::new();
    /// let names = ["anna", "peter", "bob"];
    ///
    /// for (name, status) in names.iter().with_status() {
    ///     if !status.is_first() {
    ///         s += ", ";
    ///     }
    ///
    ///     s += name;
    /// }
    ///
    /// assert_eq!(s, "anna, peter, bob");
    /// ```
    fn with_status(self) -> WithStatus<Self>;
}

impl<I: Iterator> IterStatusExt for I {
    fn with_status(self) -> WithStatus<Self> {
        WithStatus::new(self)
    }
}

/// The status of an item from an iterator (e.g. "is this the first item?").
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Status {
    first: bool,
    last: bool,
}

impl Status {
    /// Returns `true` if this is the first item of the iterator.
    ///
    /// Note that an item might simultaniously be the first and last item (if
    /// the iterator only contains one item). To check if the item is the first
    /// and and not the last, use [`Status::is_first_only`].
    ///
    /// # Example
    ///
    /// ```
    /// use splop::IterStatusExt;
    ///
    /// let v: Vec<_> = (0..4)
    ///     .with_status()
    ///     .map(|(i, status)| (i, status.is_first()))
    ///     .collect();
    ///
    /// assert_eq!(v, [
    ///     (0, true),
    ///     (1, false),
    ///     (2, false),
    ///     (3, false),
    /// ]);
    /// ```
    ///
    /// If there is only one element, this function returns `true`, as does
    /// `is_last`:
    ///
    /// ```
    /// use splop::IterStatusExt;
    ///
    /// let (_, status) = [27].iter()
    ///     .with_status()
    ///     .next()
    ///     .unwrap();
    ///
    /// assert!(status.is_first());
    /// assert!(status.is_last());
    /// ```
    pub fn is_first(&self) -> bool {
        self.first
    }

    /// Returns `true` if this is the first item and it's not the only item in
    /// the iterator.
    ///
    /// # Example
    ///
    /// ```
    /// use splop::IterStatusExt;
    ///
    /// let v: Vec<_> = (0..4)
    ///     .with_status()
    ///     .map(|(i, status)| (i, status.is_first_only()))
    ///     .collect();
    ///
    /// assert_eq!(v, [
    ///     (0, true),
    ///     (1, false),
    ///     (2, false),
    ///     (3, false),
    /// ]);
    /// ```
    ///
    /// If there is only one element, this function returns `false`:
    ///
    /// ```
    /// use splop::IterStatusExt;
    ///
    /// let (_, status) = [27].iter()
    ///     .with_status()
    ///     .next()
    ///     .unwrap();
    ///
    /// assert!(!status.is_first_only());
    /// ```
    pub fn is_first_only(&self) -> bool {
        self.first && !self.last
    }

    /// Returns `true` if this is the last item of the iterator.
    ///
    /// Note that an item might simultaniously be the last and first item (if
    /// the iterator only contains one item). To check if the item is the last
    /// and and not the first, use [`Status::is_last_only`].
    ///
    /// # Example
    ///
    /// ```
    /// use splop::IterStatusExt;
    ///
    /// let v: Vec<_> = (0..4)
    ///     .with_status()
    ///     .map(|(i, status)| (i, status.is_last()))
    ///     .collect();
    ///
    /// assert_eq!(v, [
    ///     (0, false),
    ///     (1, false),
    ///     (2, false),
    ///     (3, true),
    /// ]);
    /// ```
    ///
    /// If there is only one element, this function returns `true`, as does
    /// `is_first`:
    ///
    /// ```
    /// use splop::IterStatusExt;
    ///
    /// let (_, status) = [27].iter()
    ///     .with_status()
    ///     .next()
    ///     .unwrap();
    ///
    /// assert!(status.is_first());
    /// assert!(status.is_last());
    /// ```
    pub fn is_last(&self) -> bool {
        self.last
    }

    /// Returns `true` if this is the last item and it's not the only item in
    /// the iterator.
    ///
    /// # Example
    ///
    /// ```
    /// use splop::IterStatusExt;
    ///
    /// let v: Vec<_> = (0..4)
    ///     .with_status()
    ///     .map(|(i, status)| (i, status.is_last_only()))
    ///     .collect();
    ///
    /// assert_eq!(v, [
    ///     (0, false),
    ///     (1, false),
    ///     (2, false),
    ///     (3, true),
    /// ]);
    /// ```
    ///
    /// If there is only one element, this function returns `false`:
    ///
    /// ```
    /// use splop::IterStatusExt;
    ///
    /// let (_, status) = [27].iter()
    ///     .with_status()
    ///     .next()
    ///     .unwrap();
    ///
    /// assert!(!status.is_last_only());
    /// ```
    pub fn is_last_only(&self) -> bool {
        self.last && !self.first
    }

    /// Returns `true` if this is neither the first nor the last item.
    ///
    /// # Example
    ///
    /// ```
    /// use splop::IterStatusExt;
    ///
    /// let v: Vec<_> = (0..4)
    ///     .with_status()
    ///     .map(|(i, status)| (i, status.is_in_between()))
    ///     .collect();
    ///
    /// assert_eq!(v, [
    ///     (0, false),
    ///     (1, true),
    ///     (2, true),
    ///     (3, false),
    /// ]);
    /// ```
    pub fn is_in_between(&self) -> bool {
        !self.first && !self.last
    }
}