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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
use std::fs;
use std::io::BufReader;
use std::path::Path;
use utf8_chars::BufReadCharsExt;

pub struct Source<A: 'static>(Box<dyn FnOnce() -> Option<(A, Source<A>)> + 'static>);

impl<A: 'static> Source<A> {
    #[allow(clippy::should_implement_trait)]
    pub fn next(&mut self) -> Option<A> {
        let original = std::mem::replace(self, Source::empty());
        match original.0() {
            Some((next_element, next_source)) => {
                *self = next_source;
                Some(next_element)
            }
            None => None,
        }
    }

    pub fn has_next(&mut self) -> bool {
        match self.next() {
            Some(a) => {
                let original = std::mem::replace(self, Source::empty());
                *self = original.prepend(a);
                true
            }
            None => false,
        }
    }

    pub fn new<F: FnMut() -> Option<A> + 'static>(mut function: F) -> Source<A> {
        Source(Box::new(move || {
            function().map(|next| (next, Source::new(function)))
        }))
    }

    pub fn empty() -> Source<A> {
        Source(Box::new(|| None))
    }

    pub fn single(a: A) -> Source<A> {
        Source(Box::new(|| Some((a, Source::empty()))))
    }

    pub fn map<B, F: FnMut(A) -> B + 'static>(mut self, mut function: F) -> Source<B> {
        #[allow(clippy::redundant_closure)]
        Source::new(move || self.next().map(|x| function(x)))
    }

    pub fn filter<F: FnMut(&A) -> bool + 'static>(mut self, mut function: F) -> Source<A> {
        Source::new(move || loop {
            match self.next() {
                Some(a) if function(&a) => return Some(a),
                Some(_) => {}
                None => return None,
            }
        })
    }

    pub fn fold<Accumulator, F: FnMut(Accumulator, A) -> Accumulator>(
        self,
        initial: Accumulator,
        mut function: F,
    ) -> Accumulator {
        let mut accumulator = initial;
        for a in self {
            accumulator = function(accumulator, a);
        }
        accumulator
    }

    pub fn flat_map<B, Next: FnMut(A) -> Source<B> + 'static>(self, next: Next) -> Source<B> {
        self.map(next).flatten()
    }

    pub fn concat(self, other: Source<A>) -> Source<A> {
        Source(Box::new(move || match self.0() {
            Some((a, next)) => Some((a, next.concat(other))),
            None => other.0(),
        }))
    }

    pub fn append(self, last: A) -> Source<A> {
        self.concat(Source::single(last))
    }

    pub fn prepend(self, head: A) -> Source<A> {
        Source(Box::new(|| Some((head, self))))
    }

    pub fn count<F: Fn(A) -> bool>(self, predicate: F) -> i32 {
        self.fold(0, |count, a| if predicate(a) { count + 1 } else { count })
    }
}

impl<A, I: Iterator<Item = A> + 'static> From<I> for Source<A> {
    fn from(mut iterator: I) -> Self {
        Source::new(move || iterator.next())
    }
}

impl<A> Source<Source<A>> {
    pub fn flatten(mut self) -> Source<A> {
        let mut current = Source::empty();
        Source::new(move || loop {
            match current.next() {
                Some(a) => return Some(a),
                None => match self.next() {
                    Some(next_chunk) => {
                        current = next_chunk;
                    }
                    None => return None,
                },
            }
        })
    }
}

impl<A: Clone> Source<A> {
    pub fn peek(&mut self) -> Option<A> {
        match self.next() {
            Some(a) => {
                let original = std::mem::replace(self, Source::empty());
                *self = original.prepend(a.clone());
                Some(a)
            }
            None => None,
        }
    }

    pub fn replicate(n: u32, element: A) -> Source<A> {
        let mut counter = 0;
        Source::new(move || {
            if counter < n {
                counter += 1;
                Some(element.clone())
            } else {
                None
            }
        })
    }
}

impl Source<char> {
    pub fn read_utf8_file(file: &Path) -> Result<Source<char>, std::io::Error> {
        let mut file = BufReader::new(fs::File::open(file)?);
        Ok(Source::new(move || match file.read_char() {
            Ok(char) => char,
            Err(_) => Some(std::char::REPLACEMENT_CHARACTER),
        }))
    }
}

impl<Snippet: Into<Box<str>>> Source<Snippet> {
    pub fn join<Separator: Into<Box<str>>>(self, separator: Separator) -> String {
        let separator: &str = &separator.into();
        let mut first = true;
        let mut result = "".to_string();
        for string in self {
            if !first {
                result.push_str(separator);
            } else {
                first = false;
            }
            result.push_str(&string.into());
        }
        result
    }
}

impl<A> IntoIterator for Source<A> {
    type Item = A;
    type IntoIter = SourceIterator<A>;

    fn into_iter(self) -> SourceIterator<A> {
        SourceIterator(self)
    }
}

pub struct SourceIterator<A: 'static>(Source<A>);

impl<A> Iterator for SourceIterator<A> {
    type Item = A;

    fn next(&mut self) -> Option<A> {
        self.0.next()
    }
}

#[macro_export]
macro_rules! source {
    ($($x:expr),*) => {
        $crate::Source::from(vec![$($x),*].into_iter())
    };
    ($($x:expr,)*) => {
        source![$($x),*]
    };
    ($element:expr; $n:expr) => {
        $crate::Source::replicate($n, $element)
    };
}

impl<A: std::fmt::Debug> Source<A> {
    pub fn debug(self) -> String {
        format!("source![{}]", self.map(|x| format!("{:?}", x)).join(", "))
    }
}

#[cfg(test)]
mod test {
    use super::*;

    impl<A> Source<A> {
        pub fn to_vec(self) -> Vec<A> {
            self.into_iter().collect()
        }
    }

    mod conversions {
        use super::*;

        #[test]
        fn allows_to_convert_from_iterator() {
            let iter = vec![1, 2, 3].into_iter();
            let from_next = Source::from(iter);
            assert_eq!(from_next.to_vec(), vec![1, 2, 3]);
        }

        #[test]
        fn allows_to_convert_into_iterator() {
            let source = source!(1, 2, 3).into_iter();
            assert_eq!(source.collect::<Vec<_>>(), vec![1, 2, 3]);
        }
    }

    mod source_macro {
        use super::*;

        #[test]
        fn allows_to_convert_from_elements() {
            let source: Source<i32> = source![1, 2, 3];
            assert_eq!(source.to_vec(), vec![1, 2, 3]);
        }

        #[test]
        fn allows_to_create_empty_sources() {
            let source: Source<i32> = Source::empty();
            assert_eq!(source.to_vec(), vec![]);
        }

        #[test]
        fn allows_to_create_sources_with_one_element() {
            let source = Source::single("foo");
            assert_eq!(source.to_vec(), vec!["foo"]);
        }

        #[test]
        fn allows_to_trailing_commas() {
            let source: Source<i32> = source![1, 2, 3,];
            assert_eq!(source.to_vec(), vec![1, 2, 3]);
        }

        #[test]
        fn allows_to_replicate_a_given_element() {
            let source: Source<i32> = source![42; 3];
            assert_eq!(source.to_vec(), vec![42, 42, 42]);
        }
    }

    mod debug {
        use super::*;

        #[test]
        fn debug_converts_into_source_macro() {
            assert_eq!(source![1, 2, 3].debug(), "source![1, 2, 3]");
            let empty: Source<i32> = source![];
            assert_eq!(empty.debug(), "source![]");
        }

        #[test]
        fn debug_uses_debug() {
            assert_eq!(source!["foo", "bar"].debug(), r#"source!["foo", "bar"]"#);
        }
    }

    mod has_next {
        use super::*;

        #[test]
        fn returns_true_for_non_empty_sources() {
            let mut source = source!["foo"];
            assert_eq!(source.has_next(), true);
        }

        #[test]
        fn returns_false_when_all_elements_are_consumed() {
            let mut source = source!["foo"];
            source.next();
            assert_eq!(source.has_next(), false);
        }

        #[test]
        fn returns_false_for_empty_sources() {
            let mut source: Source<()> = source![];
            assert_eq!(source.has_next(), false);
        }

        #[test]
        fn does_not_modify_the_source_elements() {
            let mut source = source!["foo"];
            source.has_next();
            assert_eq!(source.to_vec(), vec!["foo"]);
        }
    }

    #[test]
    fn map_works() {
        let from_next: Source<i32> = source![1, 2, 3];
        let mapped = from_next.map(|x| x.pow(2));
        assert_eq!(vec![1, 4, 9], mapped.to_vec());
    }

    #[test]
    fn filter_works() {
        let source = Source::from(1..6).filter(|x| x % 2 == 1);
        assert_eq!(source.to_vec(), vec![1, 3, 5]);
    }

    #[test]
    fn fold_works() {
        let sum = Source::from(1..6).fold(0, |sum: i32, a| sum + a);
        assert_eq!(sum, 15);
    }

    #[test]
    fn flatten_works() {
        let flattened = source!["foo", "bar"]
            .map(|x| Source::from(x.chars()))
            .flatten();
        assert_eq!(vec!['f', 'o', 'o', 'b', 'a', 'r'], flattened.to_vec());
    }

    #[test]
    fn flatmap_works() {
        let source = source!["foo", "bar"].flat_map(|x| Source::from(x.chars()));
        assert_eq!(vec!['f', 'o', 'o', 'b', 'a', 'r'], source.to_vec());
    }

    #[test]
    fn concat_works() {
        let mut source = source!["foo", "bar"];
        source = source.concat(source!["baz"]);
        assert_eq!(vec!["foo", "bar", "baz"], source.to_vec());
    }

    #[test]
    fn append_works() {
        let mut source = source!["foo", "bar"];
        source = source.append("baz");
        assert_eq!(vec!["foo", "bar", "baz"], source.to_vec());
    }

    #[test]
    fn prepend_works() {
        let source = source!["bar", "baz"].prepend("foo");
        assert_eq!(vec!["foo", "bar", "baz"], source.to_vec());
    }

    mod peek {
        use super::*;

        #[test]
        fn peek_works() {
            let mut source = source!["foo", "bar"];
            assert_eq!(source.peek(), Some("foo"));
            assert_eq!(vec!["foo", "bar"], source.to_vec());
        }

        #[test]
        fn allows_to_peek_ahead() {
            let mut source = Source::from("x".chars());
            assert_eq!(source.peek(), Some('x'));
        }

        #[test]
        fn peeking_does_not_consume_chars() {
            let mut source = Source::from("x".chars());
            source.peek();
            assert_eq!(source.next(), Some('x'));
        }

        #[test]
        fn peeking_works_twice() {
            let mut source = Source::from("ab".chars());
            source.peek();
            assert_eq!(source.peek(), Some('a'));
        }

        #[test]
        fn peeking_works_after_next() {
            let mut source = Source::from("ab".chars());
            source.next();
            assert_eq!(source.peek(), Some('b'));
        }
    }

    #[test]
    fn count_works() {
        let source = source![1, 2, 3, 4, 5, 6, 7];
        assert_eq!(source.count(|x| x > 3), 4);
    }

    mod join {
        use super::*;

        #[test]
        fn works() {
            let source = source!("foo", "bar");
            assert_eq!(source.join("#"), "foo#bar");
        }

        #[test]
        fn works_with_both_str_and_string() {
            let str_source: Source<&str> = source!();
            str_source.join("");
            let str_source: Source<&str> = source!();
            str_source.join("".to_string());
            let string_source: Source<String> = source!();
            string_source.join("");
            let string_source: Source<String> = source!();
            string_source.join("".to_string());
        }

        #[test]
        fn works_with_empty_inputs() {
            assert_eq!(source!("", "foo").join("#"), "#foo");
            assert_eq!(source!("foo", "").join("#"), "foo#");
            assert_eq!(source!("", "foo", "").join("#"), "#foo#");
        }
    }
}