ordofp_core 0.1.0

OrdoFP core provides developers with HList, Disiunctio, NominataUniversalis, Universalis, and functional type classes
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
//! Result Extension Traits
//!
//! Additional methods for working with Result types in a functional style.
//!
//! # Example
//!
//! ```rust
//! use ordofp_core::easy::*;
//!
//! let result: Result<i32, &str> = Ok(42);
//!
//! // Tap into successful values
//! let tapped = result.tap(|x| println!("Got: {}", x));
//!
//! // Convert to Option
//! let opt = result.ok_or_none();
//! ```

use alloc::vec::Vec;

// =============================================================================
// Result Extension Trait
// =============================================================================

/// Extension methods for Result types.
pub trait ResultExt<T, E> {
    /// Execute a side-effect on the success value without consuming it.
    ///
    /// # Example
    ///
    /// ```rust
    /// use ordofp_core::easy::ResultExt;
    ///
    /// let result: Result<i32, &str> = Ok(42);
    /// let _ = result.tap(|x| println!("Value: {}", x));
    /// ```
    fn tap<F: FnOnce(&T)>(self, f: F) -> Self;

    /// Execute a side-effect on the error value without consuming it.
    fn tap_err<F: FnOnce(&E)>(self, f: F) -> Self;

    /// Convert Ok to Some, Err to None (alias for `ok()`).
    fn ok_or_none(self) -> Option<T>;

    /// Convert Err to Some, Ok to None (alias for `err()`).
    fn err_or_none(self) -> Option<E>;

    /// Swap Ok and Err.
    ///
    /// # Errors
    ///
    /// Returns `Err` exactly when `self` was `Ok`: the success value
    /// becomes the error and vice versa.
    fn swap(self) -> Result<E, T>;

    /// Apply a function if Ok, otherwise return the error unchanged.
    ///
    /// # Errors
    ///
    /// Propagates the existing error when `self` is `Err`; otherwise
    /// returns whatever error `f` produces from the success value.
    fn and_try<U, F>(self, f: F) -> Result<U, E>
    where
        F: FnOnce(T) -> Result<U, E>;

    /// Convert Result<Result<T, E>, E> to Result<T, E>.
    ///
    /// # Errors
    ///
    /// Returns the outer error when `self` is `Err`, or the inner error
    /// when the contained value converts into an `Err`.
    fn flatten_result(self) -> Result<T, E>
    where
        T: Into<Result<T, E>>;

    /// Get the value or a default.
    fn unwrap_or_default_with<F: FnOnce() -> T>(self, f: F) -> T;
}

impl<T, E> ResultExt<T, E> for Result<T, E> {
    #[inline]
    fn tap<F: FnOnce(&T)>(self, f: F) -> Self {
        if let Ok(ref value) = self {
            f(value);
        }
        self
    }

    #[inline]
    fn tap_err<F: FnOnce(&E)>(self, f: F) -> Self {
        if let Err(ref err) = self {
            f(err);
        }
        self
    }

    #[inline]
    fn ok_or_none(self) -> Option<T> {
        self.ok()
    }

    #[inline]
    fn err_or_none(self) -> Option<E> {
        self.err()
    }

    #[inline]
    fn swap(self) -> Result<E, T> {
        match self {
            Ok(t) => Err(t),
            Err(e) => Ok(e),
        }
    }

    #[inline]
    fn and_try<U, F>(self, f: F) -> Result<U, E>
    where
        F: FnOnce(T) -> Result<U, E>,
    {
        self.and_then(f)
    }

    #[inline]
    fn flatten_result(self) -> Result<T, E>
    where
        T: Into<Result<T, E>>,
    {
        self.and_then(core::convert::Into::into)
    }

    #[inline]
    fn unwrap_or_default_with<F: FnOnce() -> T>(self, f: F) -> T {
        self.unwrap_or_else(|_| f())
    }
}

// =============================================================================
// Option Extension Trait
// =============================================================================

/// Extension methods for Option types.
pub trait OptionExt<T> {
    /// Execute a side-effect on the value without consuming it.
    fn tap<F: FnOnce(&T)>(self, f: F) -> Self;

    /// Execute a side-effect if None.
    fn tap_none<F: FnOnce()>(self, f: F) -> Self;

    /// Convert to Result with a default error.
    ///
    /// # Errors
    ///
    /// Returns `Err(err)` when the option is `None`.
    fn ok_or_err<E>(self, err: E) -> Result<T, E>;

    /// Convert to Result with a lazily computed error.
    ///
    /// # Errors
    ///
    /// Returns `Err(f())` when the option is `None`; `f` is only
    /// invoked in that case.
    fn ok_or_else_err<E, F: FnOnce() -> E>(self, f: F) -> Result<T, E>;

    /// Filter the option with a predicate.
    fn filter_with<P: FnOnce(&T) -> bool>(self, predicate: P) -> Self;

    /// Get value or compute default.
    fn unwrap_or_compute<F: FnOnce() -> T>(self, f: F) -> T;

    /// Zip with another option.
    fn zip_with<U, R, F: FnOnce(T, U) -> R>(self, other: Option<U>, f: F) -> Option<R>;
}

impl<T> OptionExt<T> for Option<T> {
    #[inline]
    fn tap<F: FnOnce(&T)>(self, f: F) -> Self {
        if let Some(ref value) = self {
            f(value);
        }
        self
    }

    #[inline]
    fn tap_none<F: FnOnce()>(self, f: F) -> Self {
        if self.is_none() {
            f();
        }
        self
    }

    #[inline]
    fn ok_or_err<E>(self, err: E) -> Result<T, E> {
        self.ok_or(err)
    }

    #[inline]
    fn ok_or_else_err<E, F: FnOnce() -> E>(self, f: F) -> Result<T, E> {
        self.ok_or_else(f)
    }

    #[inline]
    fn filter_with<P: FnOnce(&T) -> bool>(self, predicate: P) -> Self {
        match self {
            Some(ref value) if predicate(value) => self,
            _ => None,
        }
    }

    #[inline]
    fn unwrap_or_compute<F: FnOnce() -> T>(self, f: F) -> T {
        self.unwrap_or_else(f)
    }

    #[inline]
    fn zip_with<U, R, F: FnOnce(T, U) -> R>(self, other: Option<U>, f: F) -> Option<R> {
        match (self, other) {
            (Some(a), Some(b)) => Some(f(a, b)),
            _ => None,
        }
    }
}

// =============================================================================
// Conversion Helpers
// =============================================================================

/// Convert a boolean to a Result.
///
/// # Errors
///
/// Returns `Err(err_value)` when `condition` is `false`; `ok_value` is
/// dropped in that case. Both values are eagerly constructed — use
/// [`bool_to_result_with`] when either side is expensive.
///
/// # Example
///
/// ```rust
/// use ordofp_core::easy::bool_to_result;
///
/// let result = bool_to_result(true, 42, "error");
/// assert_eq!(result, Ok(42));
/// ```
pub fn bool_to_result<T, E>(condition: bool, ok_value: T, err_value: E) -> Result<T, E> {
    if condition {
        Ok(ok_value)
    } else {
        Err(err_value)
    }
}

/// Convert a boolean to a Result with lazy evaluation.
///
/// # Errors
///
/// Returns `Err(err())` when `condition` is `false`; only the chosen
/// branch's closure is invoked.
pub fn bool_to_result_with<T, E, F, G>(condition: bool, ok: F, err: G) -> Result<T, E>
where
    F: FnOnce() -> T,
    G: FnOnce() -> E,
{
    if condition { Ok(ok()) } else { Err(err()) }
}

/// Convert a boolean to an Option.
pub fn bool_to_option<T>(condition: bool, value: T) -> Option<T> {
    if condition { Some(value) } else { None }
}

/// Convert a boolean to an Option with lazy evaluation.
pub fn bool_to_option_with<T, F: FnOnce() -> T>(condition: bool, f: F) -> Option<T> {
    if condition { Some(f()) } else { None }
}

// =============================================================================
// Collection Helpers
// =============================================================================

/// Collect results, stopping at first error.
///
/// # Errors
///
/// Returns the first `Err` yielded by the iterator; items after it are
/// not consumed. Use [`partition_results_vec`] to gather every error.
///
/// # Example
///
/// ```rust
/// use ordofp_core::easy::collect_results;
///
/// let results: Vec<Result<i32, &str>> = vec![Ok(1), Ok(2), Ok(3)];
/// let collected = collect_results(results);
/// assert_eq!(collected, Ok(vec![1, 2, 3]));
/// ```
pub fn collect_results<T, E, I>(iter: I) -> Result<Vec<T>, E>
where
    I: IntoIterator<Item = Result<T, E>>,
{
    iter.into_iter().collect()
}

/// Collect results, gathering all errors.
///
/// # Example
///
/// ```rust
/// use ordofp_core::easy::partition_results_vec;
///
/// let results: Vec<Result<i32, &str>> = vec![Ok(1), Err("a"), Ok(2), Err("b")];
/// let (successes, errors) = partition_results_vec(results);
/// assert_eq!(successes, vec![1, 2]);
/// assert_eq!(errors, vec!["a", "b"]);
/// ```
pub fn partition_results_vec<T, E, I>(iter: I) -> (Vec<T>, Vec<E>)
where
    I: IntoIterator<Item = Result<T, E>>,
{
    let mut successes = Vec::new();
    let mut errors = Vec::new();

    for result in iter {
        match result {
            Ok(t) => successes.push(t),
            Err(e) => errors.push(e),
        }
    }

    (successes, errors)
}

/// Collect options, stopping at first None.
pub fn collect_options<T, I>(iter: I) -> Option<Vec<T>>
where
    I: IntoIterator<Item = Option<T>>,
{
    iter.into_iter().collect()
}

/// Filter and map in one pass.
pub fn filter_map_results<T, U, E, F, I>(iter: I, f: F) -> Vec<U>
where
    I: IntoIterator<Item = Result<T, E>>,
    F: Fn(T) -> U,
{
    iter.into_iter()
        .filter_map(core::result::Result::ok)
        .map(f)
        .collect()
}

// =============================================================================
// Combinators
// =============================================================================

/// Try multiple operations, returning the first success.
///
/// # Example
///
/// ```rust
/// use ordofp_core::easy::first_success;
///
/// let ops: [fn() -> Result<i32, &'static str>; 3] = [
///     || Err("first failed"),
///     || Err("second failed"),
///     || Ok(42),
/// ];
/// let result = first_success(&ops);
/// assert_eq!(result, Some(42));
/// ```
pub fn first_success<T, E, F>(operations: &[F]) -> Option<T>
where
    F: Fn() -> Result<T, E>,
{
    for op in operations {
        if let Ok(value) = op() {
            return Some(value);
        }
    }
    None
}

/// Try multiple operations, returning all successes.
pub fn all_successes<T, E, F>(operations: &[F]) -> Vec<T>
where
    F: Fn() -> Result<T, E>,
{
    operations.iter().filter_map(|op| op().ok()).collect()
}

/// Execute operations until one fails.
///
/// # Errors
///
/// Returns the error of the first failing operation; operations after
/// it are not run, and the successes gathered so far are discarded.
pub fn until_failure<T, E, F>(operations: &[F]) -> Result<Vec<T>, E>
where
    F: Fn() -> Result<T, E>,
{
    let mut results = Vec::new();
    for op in operations {
        results.push(op()?);
    }
    Ok(results)
}

// =============================================================================
// Lifting
// =============================================================================

/// Lift a function to work with Results.
///
/// # Example
///
/// ```rust
/// use ordofp_core::easy::lift_result;
///
/// let add_one = |x: i32| x + 1;
/// let lifted = lift_result::<i32, i32, &str, _>(add_one);
/// assert_eq!(lifted(Ok(41)), Ok(42));
/// ```
pub fn lift_result<T, U, E, F>(f: F) -> impl Fn(Result<T, E>) -> Result<U, E>
where
    F: Fn(T) -> U,
{
    move |r| r.map(&f)
}

/// Lift a function to work with Options.
pub fn lift_option<T, U, F>(f: F) -> impl Fn(Option<T>) -> Option<U>
where
    F: Fn(T) -> U,
{
    move |o| o.map(&f)
}

/// Lift a binary function to work with Results.
pub fn lift_result2<T1, T2, U, E, F>(f: F) -> impl Fn(Result<T1, E>, Result<T2, E>) -> Result<U, E>
where
    F: Fn(T1, T2) -> U,
{
    move |r1, r2| match (r1, r2) {
        (Ok(t1), Ok(t2)) => Ok(f(t1, t2)),
        (Err(e), _) | (_, Err(e)) => Err(e),
    }
}

/// Lift a binary function to work with Options.
pub fn lift_option2<T1, T2, U, F>(f: F) -> impl Fn(Option<T1>, Option<T2>) -> Option<U>
where
    F: Fn(T1, T2) -> U,
{
    move |o1, o2| match (o1, o2) {
        (Some(t1), Some(t2)) => Some(f(t1, t2)),
        _ => None,
    }
}

// =============================================================================
// Tests
// =============================================================================

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

    #[test]
    fn test_result_tap() {
        let mut tapped = false;
        let result: Result<i32, &str> = Ok(42);
        let _ = result.tap(|_| tapped = true);
        assert!(tapped);
    }

    #[test]
    fn test_result_tap_err() {
        let mut tapped = false;
        let result: Result<i32, &str> = Err("error");
        let _ = result.tap_err(|_| tapped = true);
        assert!(tapped);
    }

    #[test]
    fn test_result_swap() {
        let ok: Result<i32, &str> = Ok(42);
        let err: Result<i32, &str> = Err("error");

        assert_eq!(ok.swap(), Err(42));
        assert_eq!(err.swap(), Ok("error"));
    }

    #[test]
    fn test_result_is_ok_and() {
        let result: Result<i32, &str> = Ok(42);
        assert!(result.is_ok_and(|x| x > 0));
        assert!(!result.is_ok_and(|x| x < 0));
    }

    #[test]
    fn test_option_tap() {
        let mut tapped = false;
        let option = Some(42);
        let _ = option.tap(|_| tapped = true);
        assert!(tapped);
    }

    #[test]
    fn test_option_tap_none() {
        let mut tapped = false;
        let option: Option<i32> = None;
        let _ = option.tap_none(|| tapped = true);
        assert!(tapped);
    }

    #[test]
    fn test_option_zip_with() {
        let a = Some(2);
        let b = Some(3);
        let result = OptionExt::zip_with(a, b, |x, y| x * y);
        assert_eq!(result, Some(6));
    }

    #[test]
    fn test_option_zip_with_none_cases() {
        assert_eq!(
            OptionExt::zip_with(Some(2_i32), None, |x, y: i32| x + y),
            None
        );
        assert_eq!(
            OptionExt::zip_with(None, Some(3_i32), |x: i32, y| x + y),
            None
        );
        assert_eq!(
            OptionExt::zip_with(None::<i32>, None::<i32>, |x, y| x + y),
            None
        );
    }

    #[test]
    fn test_bool_to_result() {
        assert_eq!(bool_to_result(true, 42, "error"), Ok(42));
        assert_eq!(bool_to_result(false, 42, "error"), Err("error"));
    }

    #[test]
    fn test_bool_to_option() {
        assert_eq!(bool_to_option(true, 42), Some(42));
        assert_eq!(bool_to_option(false, 42), None);
    }

    #[test]
    fn test_collect_results() {
        let results: Vec<Result<i32, &str>> = alloc::vec![Ok(1), Ok(2), Ok(3)];
        assert_eq!(collect_results(results), Ok(alloc::vec![1, 2, 3]));

        let results: Vec<Result<i32, &str>> = alloc::vec![Ok(1), Err("error"), Ok(3)];
        assert!(collect_results(results).is_err());
    }

    #[test]
    fn test_partition_results_vec() {
        let results: Vec<Result<i32, &str>> = alloc::vec![Ok(1), Err("a"), Ok(2), Err("b")];
        let (successes, errors) = partition_results_vec(results);
        assert_eq!(successes, alloc::vec![1, 2]);
        assert_eq!(errors, alloc::vec!["a", "b"]);
    }

    #[test]
    fn test_first_success() {
        let ops: Vec<fn() -> Result<i32, &'static str>> =
            alloc::vec![|| Err("first"), || Err("second"), || Ok(42),];
        assert_eq!(first_success(&ops), Some(42));
    }

    #[test]
    fn test_first_success_all_fail() {
        let ops: Vec<fn() -> Result<i32, &'static str>> =
            alloc::vec![|| Err("first"), || Err("second"), || Err("third")];
        assert_eq!(first_success(&ops), None);
    }

    #[test]
    fn test_all_successes_empty_slice() {
        let ops: Vec<fn() -> Result<i32, &'static str>> = alloc::vec![];
        let result: Vec<i32> = all_successes(&ops);
        assert!(result.is_empty(), "empty ops should yield empty successes");
    }

    #[test]
    fn test_until_failure_empty_slice() {
        let ops: Vec<fn() -> Result<i32, &'static str>> = alloc::vec![];
        assert_eq!(until_failure(&ops), Ok(alloc::vec![]));
    }

    #[test]
    fn test_until_failure_stops_at_first_error() {
        // The third op must never run; if it does the test panics, catching the bug.
        let ops: Vec<fn() -> Result<i32, &'static str>> =
            alloc::vec![|| Ok(1), || Err("stop"), || panic!(
                "executed past first error"
            )];
        assert_eq!(until_failure(&ops), Err("stop"));
    }

    #[test]
    fn test_lift_result() {
        let add_one = |x: i32| x + 1;
        let lifted = lift_result(add_one);
        assert_eq!(lifted(Ok(41)), Ok(42));
        assert_eq!(lifted(Err("error")), Err("error"));
    }

    #[test]
    fn test_lift_option() {
        let add_one = |x: i32| x + 1;
        let lifted = lift_option(add_one);
        assert_eq!(lifted(Some(41)), Some(42));
        assert_eq!(lifted(None), None);
    }

    #[test]
    fn test_lift_result2() {
        let add = |x: i32, y: i32| x + y;
        let lifted = lift_result2(add);
        assert_eq!(lifted(Ok(20), Ok(22)), Ok(42));
        assert_eq!(lifted(Err("error"), Ok(22)), Err("error"));
    }
}