Skip to main content

always_predicate_demo/
always_predicate_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 ******************************************************************************/
10use qubit_function::{
11    ArcPredicate,
12    BoxPredicate,
13    Predicate,
14    RcPredicate,
15};
16
17fn main() {
18    println!("=== BoxPredicate always_true/always_false Demo ===\n");
19
20    // BoxPredicate::always_true
21    let always_true: BoxPredicate<i32> = BoxPredicate::always_true();
22    println!("BoxPredicate::always_true():");
23    println!("  test(&42): {}", always_true.test(&42));
24    println!("  test(&-1): {}", always_true.test(&-1));
25    println!("  test(&0): {}", always_true.test(&0));
26    println!("  name: {:?}", always_true.name());
27
28    // BoxPredicate::always_false
29    let always_false: BoxPredicate<i32> = BoxPredicate::always_false();
30    println!("\nBoxPredicate::always_false():");
31    println!("  test(&42): {}", always_false.test(&42));
32    println!("  test(&-1): {}", always_false.test(&-1));
33    println!("  test(&0): {}", always_false.test(&0));
34    println!("  name: {:?}", always_false.name());
35
36    println!("\n=== RcPredicate always_true/always_false Demo ===\n");
37
38    // RcPredicate::always_true
39    let rc_always_true: RcPredicate<String> = RcPredicate::always_true();
40    println!("RcPredicate::always_true():");
41    println!("  test(&\"hello\"): {}", rc_always_true.test(&"hello".to_string()));
42    println!("  test(&\"world\"): {}", rc_always_true.test(&"world".to_string()));
43    println!("  name: {:?}", rc_always_true.name());
44
45    // RcPredicate::always_false
46    let rc_always_false: RcPredicate<String> = RcPredicate::always_false();
47    println!("\nRcPredicate::always_false():");
48    println!("  test(&\"hello\"): {}", rc_always_false.test(&"hello".to_string()));
49    println!("  test(&\"world\"): {}", rc_always_false.test(&"world".to_string()));
50    println!("  name: {:?}", rc_always_false.name());
51
52    // Can be cloned and reused
53    let rc_clone = rc_always_true.clone();
54    println!("\nAfter cloning, still usable:");
55    println!(
56        "  Original: test(&\"test\"): {}",
57        rc_always_true.test(&"test".to_string())
58    );
59    println!("  Clone: test(&\"test\"): {}", rc_clone.test(&"test".to_string()));
60
61    println!("\n=== ArcPredicate always_true/always_false Demo ===\n");
62
63    // ArcPredicate::always_true
64    let arc_always_true: ArcPredicate<i32> = ArcPredicate::always_true();
65    println!("ArcPredicate::always_true():");
66    println!("  test(&100): {}", arc_always_true.test(&100));
67    println!("  test(&-100): {}", arc_always_true.test(&-100));
68    println!("  name: {:?}", arc_always_true.name());
69
70    // ArcPredicate::always_false
71    let arc_always_false: ArcPredicate<i32> = ArcPredicate::always_false();
72    println!("\nArcPredicate::always_false():");
73    println!("  test(&100): {}", arc_always_false.test(&100));
74    println!("  test(&-100): {}", arc_always_false.test(&-100));
75    println!("  name: {:?}", arc_always_false.name());
76
77    println!("\n=== Combining with other predicates ===\n");
78
79    // Combining with always_true (AND)
80    let is_positive = BoxPredicate::new(|x: &i32| *x > 0);
81    let combined_and_true = is_positive.and(BoxPredicate::always_true());
82    println!("is_positive AND always_true:");
83    println!("  test(&5): {} (equivalent to is_positive)", combined_and_true.test(&5));
84    println!(
85        "  test(&-3): {} (equivalent to is_positive)",
86        combined_and_true.test(&-3)
87    );
88
89    // Combining with always_false (AND)
90    let is_positive = BoxPredicate::new(|x: &i32| *x > 0);
91    let combined_and_false = is_positive.and(BoxPredicate::always_false());
92    println!("\nis_positive AND always_false:");
93    println!("  test(&5): {} (always false)", combined_and_false.test(&5));
94    println!("  test(&-3): {} (always false)", combined_and_false.test(&-3));
95
96    // Combining with always_true (OR)
97    let is_positive = BoxPredicate::new(|x: &i32| *x > 0);
98    let combined_or_true = is_positive.or(BoxPredicate::always_true());
99    println!("\nis_positive OR always_true:");
100    println!("  test(&5): {} (always true)", combined_or_true.test(&5));
101    println!("  test(&-3): {} (always true)", combined_or_true.test(&-3));
102
103    // Combining with always_false (OR)
104    let is_positive = BoxPredicate::new(|x: &i32| *x > 0);
105    let combined_or_false = is_positive.or(BoxPredicate::always_false());
106    println!("\nis_positive OR always_false:");
107    println!("  test(&5): {} (equivalent to is_positive)", combined_or_false.test(&5));
108    println!(
109        "  test(&-3): {} (equivalent to is_positive)",
110        combined_or_false.test(&-3)
111    );
112
113    println!("\n=== Practical scenarios: Default pass/reject filters ===\n");
114
115    // Scenario 1: Default pass-all filter
116    let numbers = vec![1, 2, 3, 4, 5];
117    let pass_all = BoxPredicate::<i32>::always_true();
118    let filtered: Vec<_> = numbers.iter().copied().filter(pass_all.into_fn()).collect();
119    println!("Default pass all elements: {:?} -> {:?}", numbers, filtered);
120
121    // Scenario 2: Default reject-all filter
122    let numbers = vec![1, 2, 3, 4, 5];
123    let reject_all = BoxPredicate::<i32>::always_false();
124    let filtered: Vec<_> = numbers.iter().copied().filter(reject_all.into_fn()).collect();
125    println!("Default reject all elements: {:?} -> {:?}", numbers, filtered);
126
127    // Scenario 3: Configurable filter
128    fn configurable_filter(enable_filter: bool) -> BoxPredicate<i32> {
129        if enable_filter {
130            BoxPredicate::new(|x: &i32| *x > 3)
131        } else {
132            BoxPredicate::always_true()
133        }
134    }
135
136    let numbers = vec![1, 2, 3, 4, 5];
137
138    let filter_enabled = configurable_filter(true);
139    let filtered: Vec<_> = numbers.iter().copied().filter(filter_enabled.into_fn()).collect();
140    println!("\nFilter enabled: {:?} -> {:?}", numbers, filtered);
141
142    let filter_disabled = configurable_filter(false);
143    let filtered: Vec<_> = numbers.iter().copied().filter(filter_disabled.into_fn()).collect();
144    println!("Filter disabled: {:?} -> {:?}", numbers, filtered);
145}