Skip to main content

closure_supplier_ops_demo/
closure_supplier_ops_demo.rs

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