function_family_demo/
function_family_demo.rs1use qubit_function::{
12 BiFunction,
13 BiFunctionOnce,
14 BiMutatingFunction,
15 BiMutatingFunctionOnce,
16 BoxBiFunction,
17 BoxBiFunctionOnce,
18 BoxBiMutatingFunction,
19 BoxBiMutatingFunctionOnce,
20 BoxFunction,
21 BoxFunctionOnce,
22 BoxMutatingFunction,
23 BoxMutatingFunctionOnce,
24 BoxStatefulFunction,
25 BoxStatefulMutatingFunction,
26 Function,
27 FunctionOnce,
28 MutatingFunction,
29 MutatingFunctionOnce,
30 StatefulFunction,
31 StatefulMutatingFunction,
32};
33
34fn main() {
35 println!("=== Function Family Demo ===\n");
36
37 demo_borrowed_functions();
38 demo_mutating_functions();
39 demo_bi_functions();
40 demo_bi_mutating_functions();
41}
42
43fn demo_borrowed_functions() {
44 println!("--- Borrowed-input functions ---");
45
46 let len = BoxFunction::new(|value: &String| value.len());
47 let value = String::from("qubit");
48 println!("Function length: {}", len.apply(&value));
49 println!("Original value is still available: {value}");
50
51 let greeting = BoxFunctionOnce::new(|name: &String| format!("hello, {name}"));
52 println!("FunctionOnce greeting: {}", greeting.apply(&value));
53
54 let mut call_count = 0;
55 let mut stateful = BoxStatefulFunction::new(move |input: &i32| {
56 call_count += 1;
57 input + call_count
58 });
59 println!("StatefulFunction first call: {}", stateful.apply(&40));
60 println!("StatefulFunction second call: {}", stateful.apply(&40));
61 println!();
62}
63
64fn demo_mutating_functions() {
65 println!("--- Mutating functions ---");
66
67 let push_answer = BoxMutatingFunction::new(|items: &mut Vec<i32>| {
68 items.push(42);
69 items.len()
70 });
71 let mut items = vec![1, 2, 3];
72 println!("MutatingFunction new length: {}", push_answer.apply(&mut items));
73 println!("Items after mutation: {items:?}");
74
75 let append_once = BoxMutatingFunctionOnce::new(|text: &mut String| {
76 text.push_str(" once");
77 text.len()
78 });
79 let mut text = String::from("called");
80 println!("MutatingFunctionOnce length: {}", append_once.apply(&mut text));
81 println!("Text after one-time mutation: {text}");
82
83 let mut step = 0;
84 let mut stateful = BoxStatefulMutatingFunction::new(move |value: &mut i32| {
85 step += 1;
86 *value += step;
87 *value
88 });
89 let mut value = 40;
90 println!("StatefulMutatingFunction first call: {}", stateful.apply(&mut value));
91 println!("StatefulMutatingFunction second call: {}", stateful.apply(&mut value));
92 println!();
93}
94
95fn demo_bi_functions() {
96 println!("--- Bi-functions ---");
97
98 let describe = BoxBiFunction::new(|name: &String, score: &i32| format!("{name} scored {score}"));
99 let name = String::from("Alice");
100 let score = 98;
101 println!("BiFunction: {}", describe.apply(&name, &score));
102
103 let once = BoxBiFunctionOnce::new(|left: &String, right: &String| format!("{left}-{right}"));
104 let left = String::from("task");
105 let right = String::from("done");
106 println!("BiFunctionOnce: {}", once.apply(&left, &right));
107 println!();
108}
109
110fn demo_bi_mutating_functions() {
111 println!("--- Bi-mutating functions ---");
112
113 let move_one = BoxBiMutatingFunction::new(|source: &mut Vec<i32>, target: &mut Vec<i32>| {
114 if let Some(value) = source.pop() {
115 target.push(value);
116 }
117 target.len()
118 });
119 let mut source = vec![1, 2, 3];
120 let mut target = vec![10];
121 println!(
122 "BiMutatingFunction target length: {}",
123 move_one.apply(&mut source, &mut target)
124 );
125 println!("Source: {source:?}, target: {target:?}");
126
127 let merge_once = BoxBiMutatingFunctionOnce::new(|left: &mut String, right: &mut String| {
128 left.push_str(right);
129 right.clear();
130 left.len()
131 });
132 let mut left = String::from("hello");
133 let mut right = String::from(" world");
134 println!(
135 "BiMutatingFunctionOnce merged length: {}",
136 merge_once.apply(&mut left, &mut right)
137 );
138 println!("Left: {left}, right is empty: {}", right.is_empty());
139}