sans 0.1.0-alpha.4

Composable coroutine-based programming library for sans-io
Documentation
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
//! Transforming coroutine inputs, outputs, and return values.
//!
//! This module provides [`MapInput`], [`MapYield`], and [`MapReturn`] combinators
//! for adapting coroutines to different types.

use crate::{InitSans, Sans, step::Step};

/// Transforms input before passing it to the wrapped coroutine.
///
/// Useful for adapting between different input types or preprocessing data.
pub struct MapInput<S, F> {
    f: F,
    coro: S,
}

/// Create a coroutine that transforms input before passing it to the wrapped coroutine.
///
/// # Examples
///
/// ```
/// use sans::prelude::*;
///
/// let coro = repeat(|x: i32| x * 2);
/// let mut mapped = map_input(|s: &str| s.parse::<i32>().unwrap(), coro);
///
/// assert_eq!(mapped.next("5").unwrap_yielded(), 10);
/// ```
pub fn map_input<S, F>(f: F, coro: S) -> MapInput<S, F> {
    MapInput { f, coro }
}

/// Create a MapInput from an InitSans coroutine.
///
/// This is used when applying input transformation to a coroutine that yields immediately.
pub fn init_map_input<I1, I2, O, S, F>(f: F, coro: S) -> MapInput<S, F>
where
    S: InitSans<I2, O>,
    F: FnMut(I1) -> I2,
{
    MapInput { f, coro }
}

impl<I1, I2, O, S, F> Sans<I1, O> for MapInput<S, F>
where
    S: Sans<I2, O>,
    F: FnMut(I1) -> I2,
{
    type Return = S::Return;
    fn next(&mut self, input: I1) -> Step<O, Self::Return> {
        let i2 = (self.f)(input);
        self.coro.next(i2)
    }
}

impl<I1, I2, O, S, F> InitSans<I1, O> for MapInput<S, F>
where
    S: InitSans<I2, O>,
    F: FnMut(I1) -> I2,
{
    type Next = MapInput<S::Next, F>;

    fn init(self) -> Step<(O, Self::Next), <S::Next as Sans<I2, O>>::Return> {
        match self.coro.init() {
            Step::Yielded((o, next)) => Step::Yielded((
                o,
                MapInput {
                    f: self.f,
                    coro: next,
                },
            )),
            Step::Complete(d) => Step::Complete(d),
        }
    }
}

/// Transforms yielded values from the wrapped coroutine.
///
/// Allows converting or formatting output without changing the underlying computation.
pub struct MapYield<S, F, I, O1> {
    f: F,
    coro: S,
    _phantom: std::marker::PhantomData<(I, O1)>,
}

/// Create a coroutine that transforms yielded values from the wrapped coroutine.
///
/// # Examples
///
/// ```
/// use sans::prelude::*;
///
/// let coro = repeat(|x: i32| x * 2);
/// let mut mapped = map_yield(|y: i32| y.to_string(), coro);
///
/// assert_eq!(mapped.next(5).unwrap_yielded(), "10");
/// ```
pub fn map_yield<I, O1, O2, S, F>(f: F, coro: S) -> MapYield<S, F, I, O1>
where
    S: Sans<I, O1>,
    F: FnMut(O1) -> O2,
{
    MapYield {
        f,
        coro,
        _phantom: std::marker::PhantomData,
    }
}

/// Create a MapYield from an InitSans coroutine.
///
/// This is used when applying yield transformation to a coroutine that yields immediately.
pub fn init_map_yield<I, O1, O2, S, F>(f: F, coro: S) -> MapYield<S, F, I, O1>
where
    S: InitSans<I, O1>,
    F: FnMut(O1) -> O2,
{
    MapYield {
        f,
        coro,
        _phantom: std::marker::PhantomData,
    }
}

impl<I, O1, O2, S, F> Sans<I, O2> for MapYield<S, F, I, O1>
where
    S: Sans<I, O1>,
    F: FnMut(O1) -> O2,
{
    type Return = S::Return;
    fn next(&mut self, input: I) -> Step<O2, Self::Return> {
        match self.coro.next(input) {
            Step::Yielded(o1) => Step::Yielded((self.f)(o1)),
            Step::Complete(a) => Step::Complete(a),
        }
    }
}

impl<I, O1, O2, S, F> InitSans<I, O2> for MapYield<S, F, I, O1>
where
    S: InitSans<I, O1>,
    F: FnMut(O1) -> O2,
{
    type Next = MapYield<S::Next, F, I, O1>;

    fn init(self) -> Step<(O2, Self::Next), <S::Next as Sans<I, O1>>::Return> {
        match self.coro.init() {
            Step::Yielded((o1, next)) => {
                let mut f = self.f;
                let o2 = f(o1);
                Step::Yielded((
                    o2,
                    MapYield {
                        f,
                        coro: next,
                        _phantom: std::marker::PhantomData,
                    },
                ))
            }
            Step::Complete(d) => Step::Complete(d),
        }
    }
}

/// Transforms the final result from the wrapped coroutine.
///
/// Applied only when the computation completes, not to intermediate yields.
pub struct MapReturn<S, F> {
    f: F,
    coro: S,
}

/// Create a coroutine that transforms the final result from the wrapped coroutine.
///
/// # Examples
///
/// ```
/// use sans::prelude::*;
///
/// let coro = once(|x: i32| x + 5);
/// let mut mapped = map_return(|r: i32| r * 10, coro);
///
/// // Yield is not transformed
/// assert_eq!(mapped.next(10).unwrap_yielded(), 15);
/// // Return is transformed: 20 * 10 = 200
/// assert_eq!(mapped.next(20).unwrap_complete(), 200);
/// ```
pub fn map_return<S, F>(f: F, coro: S) -> MapReturn<S, F> {
    MapReturn { f, coro }
}

/// Create a MapReturn from an InitSans coroutine.
///
/// This is used when applying return transformation to a coroutine that yields immediately.
pub fn init_map_return<I, O, D1, D2, S, F>(f: F, coro: S) -> MapReturn<S, F>
where
    S: InitSans<I, O>,
    S::Next: Sans<I, O, Return = D1>,
    F: FnMut(D1) -> D2,
{
    MapReturn { f, coro }
}

impl<I, O, D1, D2, S, F> Sans<I, O> for MapReturn<S, F>
where
    S: Sans<I, O, Return = D1>,
    F: FnMut(D1) -> D2,
{
    type Return = D2;
    fn next(&mut self, input: I) -> Step<O, Self::Return> {
        match self.coro.next(input) {
            Step::Yielded(o) => Step::Yielded(o),
            Step::Complete(r1) => Step::Complete((self.f)(r1)),
        }
    }
}

impl<I, O, D1, D2, S, F> InitSans<I, O> for MapReturn<S, F>
where
    S: InitSans<I, O>,
    S::Next: Sans<I, O, Return = D1>,
    F: FnMut(D1) -> D2,
{
    type Next = MapReturn<S::Next, F>;

    fn init(self) -> Step<(O, Self::Next), D2> {
        match self.coro.init() {
            Step::Yielded((o, next)) => Step::Yielded((
                o,
                MapReturn {
                    f: self.f,
                    coro: next,
                },
            )),
            Step::Complete(d1) => {
                let mut f = self.f;
                Step::Complete(f(d1))
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::InitSans;
    use crate::build::repeat;

    #[test]
    fn test_map_input_and_map_yield_pipeline() {
        let mut total = 0_i64;
        let init_coro = (
            0_i64,
            repeat(move |delta: i64| {
                total += delta;
                total
            }),
        )
            .map_input(|cmd: &str| -> i64 {
                let mut parts = cmd.split_whitespace();
                let op = parts.next().expect("operation must exist");
                let amount: i64 = parts
                    .next()
                    .expect("amount must exist")
                    .parse()
                    .expect("amount must parse");
                match op {
                    "add" => amount,
                    "sub" => -amount,
                    _ => panic!("unsupported op: {op}"),
                }
            })
            .map_yield(|value: i64| format!("total={value}"));

        let (initial_total, mut coro) = init_coro.init().unwrap_yielded();

        assert_eq!("total=0", initial_total);
        assert_eq!("total=5", coro.next("add 5").unwrap_yielded());
        assert_eq!("total=2", coro.next("sub 3").unwrap_yielded());
        assert_eq!("total=7", coro.next("add 5").unwrap_yielded());
    }

    #[test]
    fn test_map_input_basic() {
        use crate::build::repeat;
        let coro = repeat(|x: i32| x * 2);
        let mut mapped = map_input(|s: &str| s.parse::<i32>().unwrap(), coro);

        assert_eq!(mapped.next("5").unwrap_yielded(), 10);
        assert_eq!(mapped.next("7").unwrap_yielded(), 14);
        assert_eq!(mapped.next("10").unwrap_yielded(), 20);
    }

    #[test]
    fn test_map_input_with_once() {
        use crate::build::once;
        let coro = once(|x: i32| x + 100);
        let mut mapped = map_input(|s: String| s.len() as i32, coro);

        // First input: "hello".len() = 5, yields 5 + 100 = 105
        assert_eq!(mapped.next("hello".to_string()).unwrap_yielded(), 105);
        // Second input: "world".len() = 5, completes with 5
        assert_eq!(mapped.next("world".to_string()).unwrap_complete(), 5);
    }

    #[test]
    fn test_map_input_preserves_return() {
        use crate::build::once;
        let coro = once(|x: i32| x * 2);
        let mut mapped = map_input(|x: i32| x + 1, coro);

        // Input 5 -> 6, yields 12
        mapped.next(5).unwrap_yielded();
        // Input 10 -> 11, completes with 11
        assert_eq!(mapped.next(10).unwrap_complete(), 11);
    }

    #[test]
    fn test_map_yield_basic() {
        use crate::build::repeat;
        let coro = repeat(|x: i32| x * 2);
        let mut mapped = map_yield(|y: i32| y.to_string(), coro);

        assert_eq!(mapped.next(5).unwrap_yielded(), "10");
        assert_eq!(mapped.next(7).unwrap_yielded(), "14");
        assert_eq!(mapped.next(100).unwrap_yielded(), "200");
    }

    #[test]
    fn test_map_yield_with_once() {
        use crate::build::once;
        let coro = once(|x: i32| x + 10);
        let mut mapped = map_yield(|y: i32| format!("result={}", y), coro);

        assert_eq!(mapped.next(5).unwrap_yielded(), "result=15");
        assert_eq!(mapped.next(20).unwrap_complete(), 20);
    }

    #[test]
    fn test_map_yield_preserves_return() {
        use crate::build::once;
        let coro = once(|x: i32| x * 2);
        let mut mapped = map_yield(|y: i32| y as f64, coro);

        // Yield is transformed to f64
        assert_eq!(mapped.next(5).unwrap_yielded(), 10.0);
        // Return is NOT transformed (still i32)
        assert_eq!(mapped.next(7).unwrap_complete(), 7);
    }

    #[test]
    fn test_map_return_basic() {
        use crate::build::once;
        let coro = once(|x: i32| x + 5);
        let mut mapped = map_return(|r: i32| r * 10, coro);

        // Yield is not transformed
        assert_eq!(mapped.next(10).unwrap_yielded(), 15);
        // Return is transformed: 20 * 10 = 200
        assert_eq!(mapped.next(20).unwrap_complete(), 200);
    }

    #[test]
    fn test_map_return_with_repeat() {
        use crate::build::repeat;
        // repeat never completes, so this just demonstrates the type change
        let coro = repeat(|x: i32| x + 1);
        let _mapped = map_return(|r: i32| r.to_string(), coro);
        // We can't test completion, but we can verify it compiles with transformed return type
    }

    #[test]
    fn test_map_return_yield_passthrough() {
        use crate::build::once;
        let coro = once(|x: i32| x * 2);
        let mut mapped = map_return(|r: i32| format!("done:{}", r), coro);

        // First: yields 5 * 2 = 10
        assert_eq!(mapped.next(5).unwrap_yielded(), 10);
        // Second: once completes with 7, return is transformed
        assert_eq!(mapped.next(7).unwrap_complete(), "done:7");
    }

    #[test]
    fn test_map_return_type_conversion() {
        use crate::build::once;
        let coro = once(|x: i32| x + 1);
        let mut mapped = map_return(|r: i32| (r as f64, r * 2), coro);

        mapped.next(5).unwrap_yielded(); // 6
        // Return is transformed to tuple
        assert_eq!(mapped.next(10).unwrap_complete(), (10.0, 20));
    }

    #[test]
    fn test_all_three_maps_combined() {
        use crate::build::once;
        // Input: &str -> parse to i32
        // Yield: i32 -> format as string
        // Return: i32 -> convert to f64
        let coro = once(|x: i32| x * 2);
        let mut mapped = map_return(
            |r: i32| r as f64,
            map_yield(
                |y: i32| format!("yielded:{}", y),
                map_input(|s: &str| s.parse::<i32>().unwrap(), coro),
            ),
        );

        // Input "5" -> 5, yields 10 -> "yielded:10"
        assert_eq!(mapped.next("5").unwrap_yielded(), "yielded:10");
        // Input "7" -> 7, completes with 7 -> 7.0
        assert_eq!(mapped.next("7").unwrap_complete(), 7.0);
    }

    #[test]
    fn test_map_input_multiple_transformations() {
        use crate::build::repeat;
        let coro = repeat(|x: i32| x + 1);
        // Double map_input: String -> usize (len) -> i32
        let mut mapped = map_input(|s: String| s.len(), map_input(|n: usize| n as i32, coro));

        // Input "hello" -> len=5 -> 5 + 1 = 6
        assert_eq!(mapped.next("hello".to_string()).unwrap_yielded(), 6);
        // Input "a" -> len=1 -> 1 + 1 = 2
        assert_eq!(mapped.next("a".to_string()).unwrap_yielded(), 2);
    }
}