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
//! Easy API - Simplified Interface for `OrdoFP`
//!
//! This module provides a beginner-friendly API that hides the complexity
//! of effect types, row polymorphism, and type-level programming.
//!
//! # Philosophy
//!
//! The Easy API follows these principles:
//!
//! 1. **Zero type annotations required** - Types are inferred
//! 2. **Familiar patterns** - Looks like regular Rust
//! 3. **Escape hatches** - Can drop down to full API when needed
//!
//! # Usage
//!
//! ```rust
//! use ordofp_core::easy::*;
//!
//! // Simple state management
//! let result = run_with_state(0, |state| {
//!     *state += 1;
//!     *state * 2
//! });
//! assert_eq!(result, 2);
//!
//! // Reader pattern for configuration
//! #[derive(Default)]
//! struct Config {
//!     timeout_ms: u64,
//! }
//!
//! let result = run_with_config(&Config::default(), |config: &Config| {
//!     config.timeout_ms
//! });
//! assert_eq!(result, 0);
//!
//! // Error handling
//! let result: Result<i32, String> = run_with_error(|| {
//!     Ok(42)
//! });
//! assert_eq!(result, Ok(42));
//! ```

mod error;
mod io;
mod reader;
mod result_ext;
mod state;

pub use error::*;
pub use io::*;
pub use reader::*;
pub use result_ext::*;
pub use state::*;

// =============================================================================
// Do-Notation Style Combinators
// =============================================================================

/// Chain computations together, passing the result of each to the next.
///
/// This provides a do-notation-like experience without macros.
///
/// # Example
///
/// ```rust
/// use ordofp_core::easy::*;
///
/// let result = chain(
///     || 1,
///     |x| x + 1,
///     |x| x * 2,
/// );
/// assert_eq!(result, 4);
/// ```
pub fn chain<A, B, C, F1, F2, F3>(first: F1, second: F2, third: F3) -> C
where
    F1: FnOnce() -> A,
    F2: FnOnce(A) -> B,
    F3: FnOnce(B) -> C,
{
    third(second(first()))
}

/// Chain two computations.
pub fn chain2<A, B, F1, F2>(first: F1, second: F2) -> B
where
    F1: FnOnce() -> A,
    F2: FnOnce(A) -> B,
{
    second(first())
}

/// Chain four computations.
pub fn chain4<A, B, C, D, F1, F2, F3, F4>(first: F1, second: F2, third: F3, fourth: F4) -> D
where
    F1: FnOnce() -> A,
    F2: FnOnce(A) -> B,
    F3: FnOnce(B) -> C,
    F4: FnOnce(C) -> D,
{
    fourth(third(second(first())))
}

// =============================================================================
// Pure Computations
// =============================================================================

/// Lift a pure value into a computation context.
///
/// # Example
///
/// ```rust
/// use ordofp_core::easy::pure;
///
/// let x = pure(42);
/// assert_eq!(x, 42);
/// ```
#[inline]
pub fn pure<T>(value: T) -> T {
    value
}

/// Sequence two computations, discarding the first result.
///
/// # Example
///
/// ```rust
/// use ordofp_core::easy::sequence;
///
/// let result = sequence(
///     || println!("first"),
///     || 42,
/// );
/// assert_eq!(result, 42);
/// ```
pub fn sequence<A, B, F1, F2>(first: F1, second: F2) -> B
where
    F1: FnOnce() -> A,
    F2: FnOnce() -> B,
{
    let _ = first();
    second()
}

/// Apply a function to the result of a computation.
///
/// # Example
///
/// ```rust
/// use ordofp_core::easy::map;
///
/// let result = map(|| 21, |x| x * 2);
/// assert_eq!(result, 42);
/// ```
pub fn map<A, B, F, G>(computation: F, mapper: G) -> B
where
    F: FnOnce() -> A,
    G: FnOnce(A) -> B,
{
    mapper(computation())
}

/// Flatten nested computations.
///
/// # Example
///
/// ```rust
/// use ordofp_core::easy::flatten;
///
/// let result = flatten(|| || 42);
/// assert_eq!(result, 42);
/// ```
pub fn flatten<A, F, G>(computation: F) -> A
where
    F: FnOnce() -> G,
    G: FnOnce() -> A,
{
    computation()()
}

// =============================================================================
// Tuple Combinators
// =============================================================================

/// Run two computations and combine their results into a tuple.
///
/// # Example
///
/// ```rust
/// use ordofp_core::easy::both;
///
/// let (a, b) = both(|| 1, || 2);
/// assert_eq!((a, b), (1, 2));
/// ```
pub fn both<A, B, F1, F2>(first: F1, second: F2) -> (A, B)
where
    F1: FnOnce() -> A,
    F2: FnOnce() -> B,
{
    (first(), second())
}

/// Run three computations and combine their results.
pub fn all3<A, B, C, F1, F2, F3>(f1: F1, f2: F2, f3: F3) -> (A, B, C)
where
    F1: FnOnce() -> A,
    F2: FnOnce() -> B,
    F3: FnOnce() -> C,
{
    (f1(), f2(), f3())
}

/// Run four computations and combine their results.
pub fn all4<A, B, C, D, F1, F2, F3, F4>(f1: F1, f2: F2, f3: F3, f4: F4) -> (A, B, C, D)
where
    F1: FnOnce() -> A,
    F2: FnOnce() -> B,
    F3: FnOnce() -> C,
    F4: FnOnce() -> D,
{
    (f1(), f2(), f3(), f4())
}

// =============================================================================
// Conditional Combinators
// =============================================================================

/// Conditional computation based on a predicate.
///
/// # Example
///
/// ```rust
/// use ordofp_core::easy::when;
///
/// let result = when(true, || 42, || 0);
/// assert_eq!(result, 42);
/// ```
pub fn when<A, F1, F2>(condition: bool, if_true: F1, if_false: F2) -> A
where
    F1: FnOnce() -> A,
    F2: FnOnce() -> A,
{
    if condition { if_true() } else { if_false() }
}

/// Execute a computation only if a condition is true.
///
/// # Example
///
/// ```rust
/// use ordofp_core::easy::when_true;
///
/// let result = when_true(true, || 42);
/// assert_eq!(result, Some(42));
/// ```
pub fn when_true<A, F>(condition: bool, computation: F) -> Option<A>
where
    F: FnOnce() -> A,
{
    if condition { Some(computation()) } else { None }
}

/// Execute a computation only if a condition is false.
pub fn when_false<A, F>(condition: bool, computation: F) -> Option<A>
where
    F: FnOnce() -> A,
{
    (!condition).then(computation)
}

// =============================================================================
// Loop Combinators
// =============================================================================

/// Repeat a computation N times, collecting results.
///
/// # Example
///
/// ```rust
/// use ordofp_core::easy::repeat;
///
/// let results = repeat(3, |i| i * 2);
/// assert_eq!(results, vec![0, 2, 4]);
/// ```
pub fn repeat<A, F>(count: usize, computation: F) -> alloc::vec::Vec<A>
where
    F: FnMut(usize) -> A,
{
    (0..count).map(computation).collect()
}

/// Iterate while a condition is true.
///
/// # Example
///
/// ```rust
/// use core::cell::Cell;
/// use ordofp_core::easy::iterate_while;
///
/// let i = Cell::new(0);
/// let result = iterate_while(|| i.get() < 5, || { i.set(i.get() + 1); i.get() });
/// assert_eq!(result, vec![1, 2, 3, 4, 5]);
/// ```
pub fn iterate_while<A, P, F>(mut predicate: P, mut computation: F) -> alloc::vec::Vec<A>
where
    P: FnMut() -> bool,
    F: FnMut() -> A,
{
    let mut results = alloc::vec::Vec::new();
    while predicate() {
        results.push(computation());
    }
    results
}

/// Fold over a range.
///
/// # Example
///
/// ```rust
/// use ordofp_core::easy::fold_range;
///
/// let sum = fold_range(0..5, 0, |acc, i| acc + i);
/// assert_eq!(sum, 10);
/// ```
pub fn fold_range<A, F>(range: core::ops::Range<usize>, init: A, mut folder: F) -> A
where
    F: FnMut(A, usize) -> A,
{
    let mut acc = init;
    for i in range {
        acc = folder(acc, i);
    }
    acc
}

// =============================================================================
// Resource Management
// =============================================================================

/// Execute a computation with a resource, closing it on the normal path.
///
/// Similar in shape to try-with-resources, but **not panic-safe**: `close`
/// runs only when `use_resource` returns normally. If `use_resource` panics,
/// `close` is skipped and the resource is simply dropped during unwinding —
/// any cleanup beyond `Drop` will not happen. For guaranteed cleanup use a
/// RAII guard type instead.
///
/// # Example
///
/// ```rust,no_run
/// // Uses real file IO, so this example is `no_run` (the file may not exist
/// // and the doctest binary is not built with the crate's own `std` feature).
/// use ordofp_core::easy::with_resource;
/// use std::fs::File;
/// use std::io::Read;
///
/// let contents: String = with_resource(
///     || File::open("test.txt").expect("failed to open file"),
///     |file: &File| {
///         let mut file = file.try_clone().expect("failed to clone handle");
///         let mut buf = String::new();
///         file.read_to_string(&mut buf).expect("failed to read");
///         buf
///     },
///     |_file| {},
/// );
/// println!("{contents}");
/// ```
pub fn with_resource<R, A, Open, Use, Close>(open: Open, use_resource: Use, close: Close) -> A
where
    Open: FnOnce() -> R,
    Use: FnOnce(&R) -> A,
    Close: FnOnce(R),
{
    let resource = open();
    let result = use_resource(&resource);
    close(resource);
    result
}

/// Execute a computation with a mutable resource.
///
/// Same caveat as [`with_resource`]: `close` is skipped if `use_resource`
/// panics — cleanup happens only on the normal return path.
pub fn with_resource_mut<R, A, Open, Use, Close>(open: Open, use_resource: Use, close: Close) -> A
where
    Open: FnOnce() -> R,
    Use: FnOnce(&mut R) -> A,
    Close: FnOnce(R),
{
    let mut resource = open();
    let result = use_resource(&mut resource);
    close(resource);
    result
}

// =============================================================================
// Validation Combinators
// =============================================================================

/// Validate a value with a predicate, returning an Option.
///
/// # Example
///
/// ```rust
/// use ordofp_core::easy::validate;
///
/// let valid = validate(42, |x| *x > 0);
/// assert_eq!(valid, Some(42));
///
/// let invalid = validate(-1, |x| *x > 0);
/// assert_eq!(invalid, None);
/// ```
pub fn validate<T, P>(value: T, predicate: P) -> Option<T>
where
    P: FnOnce(&T) -> bool,
{
    if predicate(&value) { Some(value) } else { None }
}

/// Validate a value, returning a Result with an error message.
///
/// # Errors
///
/// Returns `Err(error)` when `predicate(&value)` is `false`; the value
/// is dropped in that case.
///
/// # Example
///
/// ```rust
/// use ordofp_core::easy::validate_or;
///
/// let result: Result<i32, &str> = validate_or(42, |x| *x > 0, "must be positive");
/// assert!(result.is_ok());
/// ```
pub fn validate_or<T, E, P>(value: T, predicate: P, error: E) -> Result<T, E>
where
    P: FnOnce(&T) -> bool,
{
    if predicate(&value) {
        Ok(value)
    } else {
        Err(error)
    }
}

/// A single validation: a predicate over `&T` paired with the error to emit
/// when the predicate fails.
pub type Validation<T, E> = (fn(&T) -> bool, E);

/// Validate multiple conditions, collecting all errors.
///
/// # Errors
///
/// Every predicate is run against `value`; if any fail, returns a `Vec`
/// with the error of each failed validation in slice order. All
/// validations are checked — this accumulates rather than
/// short-circuiting on the first failure.
///
/// # Example
///
/// ```rust
/// use ordofp_core::easy::validate_all;
///
/// let result = validate_all(
///     42,
///     &[
///         (|x| *x > 0, "must be positive"),
///         (|x: &i32| *x < 100, "must be less than 100"),
///     ],
/// );
/// assert!(result.is_ok());
/// ```
pub fn validate_all<T, E: Clone>(
    value: T,
    validations: &[Validation<T, E>],
) -> Result<T, alloc::vec::Vec<E>> {
    let errors: alloc::vec::Vec<E> = validations
        .iter()
        .filter(|(pred, _)| !pred(&value))
        .map(|(_, err)| err.clone())
        .collect();

    if errors.is_empty() {
        Ok(value)
    } else {
        Err(errors)
    }
}

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

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

    #[test]
    fn test_chain() {
        let result = chain(|| 1, |x| x + 1, |x| x * 2);
        assert_eq!(result, 4);
    }

    #[test]
    fn test_chain2() {
        let result = chain2(|| 10, |x| x * 2);
        assert_eq!(result, 20);
    }

    #[test]
    fn test_pure() {
        assert_eq!(pure(42), 42);
    }

    #[test]
    fn test_map() {
        let result = map(|| 21, |x| x * 2);
        assert_eq!(result, 42);
    }

    #[test]
    fn test_both() {
        let (a, b) = both(|| 1, || 2);
        assert_eq!((a, b), (1, 2));
    }

    #[test]
    fn test_when() {
        assert_eq!(when(true, || 1, || 0), 1);
        assert_eq!(when(false, || 1, || 0), 0);
    }

    #[test]
    fn test_when_true() {
        assert_eq!(when_true(true, || 42), Some(42));
        assert_eq!(when_true(false, || 42), None);
    }

    #[test]
    fn test_repeat() {
        let results = repeat(3, |i| i * 2);
        assert_eq!(results, alloc::vec![0, 2, 4]);
    }

    #[test]
    fn test_fold_range() {
        let sum = fold_range(0..5, 0, |acc, i| acc + i);
        assert_eq!(sum, 10);
    }

    #[test]
    fn test_validate() {
        assert_eq!(validate(42, |x| *x > 0), Some(42));
        assert_eq!(validate(-1, |x: &i32| *x > 0), None);
    }

    #[test]
    fn test_validate_or() {
        let result: Result<i32, &str> = validate_or(42, |x| *x > 0, "must be positive");
        assert!(result.is_ok());

        let result: Result<i32, &str> = validate_or(-1, |x| *x > 0, "must be positive");
        assert!(result.is_err());
    }
}