closure_supplier_ops_demo/
closure_supplier_ops_demo.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025.
4 *    3-Prism Co. Ltd.
5 *
6 *    All rights reserved.
7 *
8 ******************************************************************************/
9
10/**
11 * Demonstrates various operations on closures using Supplier trait.
12 *
13 * This example showcases how to use the Supplier trait with closures,
14 * including mapping, filtering, zipping, and memoization.
15 */
16use prism3_function::{FnSupplierOps, Supplier};
17
18fn main() {
19    println!("=== Closure Supplier Operations Demo ===\n");
20
21    // 1. FnMut closure using map
22    println!("1. FnMut closure using map:");
23    let mut counter = 0;
24    let mut mapped = (move || {
25        counter += 1;
26        counter
27    })
28    .map(|x| x * 2);
29
30    println!("   First call: {}", mapped.get());
31    println!("   Second call: {}\n", mapped.get());
32
33    // 2. FnMut closure using filter
34    println!("2. FnMut closure using filter:");
35    let mut counter2 = 0;
36    let mut filtered = (move || {
37        counter2 += 1;
38        counter2
39    })
40    .filter(|x| x % 2 == 0);
41
42    println!("   First call (odd number): {:?}", filtered.get());
43    println!("   Second call (even number): {:?}\n", filtered.get());
44
45    // 3. FnMut closure using memoize
46    println!("3. FnMut closure using memoize:");
47    let mut call_count = 0;
48    let mut memoized = (move || {
49        call_count += 1;
50        println!("   Underlying function called {} times", call_count);
51        42
52    })
53    .memoize();
54
55    println!("   First call: {}", memoized.get());
56    println!("   Second call: {}", memoized.get());
57    println!("   Third call: {}\n", memoized.get());
58
59    // 4. Fn closure using map (Fn also implements FnMut, so can use FnSupplierOps)
60    println!("4. Fn closure using map:");
61    let mut mapped_readonly = (|| 10).map(|x| x * 3).map(|x| x + 5);
62    println!("   Result: {}\n", mapped_readonly.get());
63
64    // 5. Fn closure using filter (Fn also implements FnMut, so can use FnSupplierOps)
65    println!("5. Fn closure using filter:");
66    let mut filtered_readonly = (|| 42).filter(|x| x % 2 == 0);
67    println!("   Filtered even number: {:?}\n", filtered_readonly.get());
68
69    // 6. Chained operations
70    println!("6. Chained operations:");
71    let mut counter3 = 0;
72    let mut chained = (move || {
73        counter3 += 1;
74        counter3
75    })
76    .map(|x| x * 2)
77    .filter(|x| *x > 5)
78    .map(|opt: Option<i32>| opt.unwrap_or(0));
79
80    println!("   First call: {}", chained.get()); // 2, filtered out
81    println!("   Second call: {}", chained.get()); // 4, filtered out
82    println!("   Third call: {}", chained.get()); // 6, passed
83    println!("   Fourth call: {}\n", chained.get()); // 8, passed
84
85    println!("=== Demo completed ===");
86}