1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/// Creates two normal [`channels`] and connects them to a new [`ThreadPool`] to create a
/// multi-producer multi-consumer multi-threaded lambda-channel.
///
/// [`channels`]: channel
/// [`ThreadPool`]: thread::ThreadPool
///
/// # Examples
///
/// ```
/// use crossbeam_channel::RecvError;
/// use lambda_channel::new_lambda_channel;
///
/// fn fib(_: &Option<()>, n: i32) -> u64 {
/// if n <= 1 {
/// n as u64
/// } else {
/// fib(&None, n - 1) + fib(&None, n - 2)
/// }
/// }
///
/// let (s, r, _p) = new_lambda_channel(None, None, None, fib);
/// s.send(20).unwrap();
/// assert_eq!(r.recv(), Ok(6765));
///
/// s.send(10).unwrap();
/// drop(s);
/// assert_eq!(r.recv(), Ok(55));
/// assert_eq!(r.recv(), Err(RecvError));
/// ```
/// Connects two existing [`channels`] to a new [`ThreadPool`] to
/// create a multi-producer multi-consumer multi-threaded lambda-channel.
/// While it is not required for the input channel of the lambda-channel to have the output
/// channel as a dependency, it may lead to undesired termination behaviors.
///
/// [`channels`]: channel
/// [`ThreadPool`]: thread::ThreadPool
///
/// # Examples
///
/// ```
/// use lambda_channel::new_lambda_channel_with_input_and_output;
/// use lambda_channel::channel::{new_channel, new_channel_with_dependency};
///
/// fn convert_units(c: &f64, u: f64) -> f64 {
/// c * u
/// }
///
/// fn approx_eq(v: f64, c: f64) -> bool {
/// if (v > (c + 0.000001)) || (v < (c - 0.000001)) {
/// return false;
/// }
/// true
/// }
///
/// let (s_km, r_km) = new_channel(None);
/// let (s_mile, r_mile_to_km) = new_channel_with_dependency(None, &s_km, &r_km);
/// let (s_nautical_mile, r_nautical_mile_to_km) = new_channel_with_dependency(None, &s_km, &r_km);
///
/// let _p_mile_to_km = new_lambda_channel_with_input_and_output(r_mile_to_km, s_km.clone(), 1.60934, convert_units);
/// let _p_nautical_mile_to_km = new_lambda_channel_with_input_and_output(r_nautical_mile_to_km, s_km, 1.852, convert_units);
///
/// s_mile.send(12.0).unwrap();
/// s_nautical_mile.send(2.0).unwrap();
///
/// let msg1 = r_km.recv().unwrap();
/// let msg2 = r_km.recv().unwrap();
/// assert!(approx_eq(msg1 + msg2, 23.01608));
/// ```
/// Creates a normal [`channel`] and connects it to a new [`ThreadPool`] to create a
/// multi-producer multi-threaded lambda-sink.
///
/// [`ThreadPool`]: thread::ThreadPool
///
/// # Examples
///
/// ```
/// use lambda_channel::new_lambda_sink;
///
/// fn do_something(_: &Option<()>, n: i32) {
/// println!("Do something without output: {}", n);
/// }
///
/// let (s, _p) = new_lambda_sink(Some(0), None, do_something);
/// s.send(1).unwrap();
/// ```
/// Connects an existing [`channel`] to a new [`ThreadPool`] to create a
/// multi-producer multi-threaded lambda-sink.
///
/// [`ThreadPool`]: thread::ThreadPool
///
/// # Examples
///
/// ```
/// use std::collections::HashMap;
/// use rand::{seq::SliceRandom, thread_rng};
///
/// use lambda_channel::new_lambda_sink_with_input_from;
/// use lambda_channel::channel::new_channel;
///
/// fn generate_cipher_map() -> HashMap<char, char> {
/// let mut rng = thread_rng();
/// let mut alphanumeric: Vec<char> = Vec::new();
/// alphanumeric.extend('0'..='9');
/// alphanumeric.extend('a'..='z');
/// alphanumeric.extend('A'..='Z');
///
/// let mut shuffled_alphanumeric = alphanumeric.clone();
/// shuffled_alphanumeric.shuffle(&mut rng);
///
/// let mut cipher_map = HashMap::new();
/// for (i, &char_val) in alphanumeric.iter().enumerate() {
/// cipher_map.insert(char_val, shuffled_alphanumeric[i]);
/// }
/// cipher_map
/// }
///
/// fn encode_text(cipher: &HashMap<char, char>, text: String) {
/// let mut encoded_text = String::new();
/// println!("Encoding: {}", text);
/// for c in text.chars() {
/// if let Some(v) = cipher.get(&c) {
/// encoded_text.push(*v)
/// } else {
/// encoded_text.push(c)
/// }
/// }
/// println!("Cipher Text: {}", encoded_text);
/// }
///
/// let cipher_map = generate_cipher_map();
/// let (s, r) = new_channel(Some(0));
/// let _p = new_lambda_sink_with_input_from(r, cipher_map, encode_text);
/// s.send("This is a test!".to_string()).unwrap();
/// ```