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
// Empty-source fallback and finite repetition.
//
// Spliced into the `FluentIter` trait by `build.rs`. Names resolve in
// the crate-root scope of `lib.rs`, exactly as before the split.
/// ⇔ `.whenEmpty(fallback)`: yield from the source; if it produces
/// **nothing**, yield the fallback instead. A non-empty source never
/// touches the fallback iterable.
///
/// Unlike JS the fallback must yield the same `Item` type — map it
/// first if it doesn't.
///
/// ```
/// use fluent_iterable::FluentIter;
///
/// let empty: [i32; 0] = [];
/// let out: Vec<i32> = empty.into_iter().when_empty([7, 8]).collect();
/// assert_eq!(out, vec![7, 8]);
///
/// let out: Vec<i32> = [1].into_iter().when_empty([7, 8]).collect();
/// assert_eq!(out, vec![1]);
/// ```
/// ⇔ `.repeat(n)`: yield the whole sequence `n` times (first pass
/// streams and caches, later passes replay). `n == 0` yields nothing
/// and never consumes the source. Unlike `std`'s nightly-only `cycle`,
/// this is finite.
///
/// ```
/// use fluent_iterable::FluentIter;
///
/// let out: Vec<i32> = [1, 2].into_iter().repeat(3).collect();
/// assert_eq!(out, vec![1, 2, 1, 2, 1, 2]);
///
/// let out: Vec<i32> = [1, 2].into_iter().repeat(0).collect();
/// assert!(out.is_empty());
/// ```