Skip to main content

closure_supplier_ops_demo/
closure_supplier_ops_demo.rs

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