Skip to main content

std_mel/ops/
mod.rs

1use melodium_core::*;
2use melodium_macro::{check, mel_function, mel_treatment};
3
4pub mod bin;
5pub mod float;
6pub mod num;
7pub mod option;
8pub mod vec;
9
10/// Conditionally return `a` or `b`
11#[mel_function(
12    generic T ()
13)]
14pub fn condition(condition: bool, a: T, b: T) -> T {
15    if condition {
16        a
17    } else {
18        b
19    }
20}
21
22/// Return whether `a` is equal to `b`
23#[mel_function(
24    generic T (PartialEquality)
25)]
26pub fn equal(a: T, b: T) -> bool {
27    a.partial_equality_eq(&b)
28}
29
30/// Return whether `a` is different `b`
31#[mel_function(
32    generic T (PartialEquality)
33)]
34pub fn not_equal(a: T, b: T) -> bool {
35    a.partial_equality_ne(&b)
36}
37
38/// Determine whether `a` is equal to `b`
39#[mel_treatment(
40    generic T (PartialEquality)
41    input a Stream<T>
42    input b Stream<T>
43    output result Stream<bool>
44)]
45pub async fn equal() {
46    while let (Ok(a), Ok(b)) = (a.recv_one().await, b.recv_one().await) {
47        check!(result.send_one(a.partial_equality_eq(&b).into()).await)
48    }
49}
50
51/// Determine whether `a` is different from `b`
52#[mel_treatment(
53    generic T (PartialEquality)
54    input a Stream<T>
55    input b Stream<T>
56    output result Stream<bool>
57)]
58pub async fn not_equal() {
59    while let (Ok(a), Ok(b)) = (a.recv_one().await, b.recv_one().await) {
60        check!(result.send_one(a.partial_equality_ne(&b).into()).await)
61    }
62}
63
64/// Tells whether `a` is strictly greater than `b`.
65#[mel_function(
66    generic T (PartialOrder)
67)]
68pub fn gt(a: T, b: T) -> bool {
69    a.partial_order_gt(&b)
70}
71
72/// Determine whether `a` is strictly greater than `b`
73#[mel_treatment(
74    generic T (PartialOrder)
75    input a Stream<T>
76    input b Stream<T>
77    output is Stream<bool>
78)]
79pub async fn greater_than() {
80    while let (Ok(a), Ok(b)) = (a.recv_one().await, b.recv_one().await) {
81        check!(is.send_one(a.partial_order_gt(&b).into()).await)
82    }
83}
84
85/// Tells whether `a` is greater or equal to `b`.
86#[mel_function(
87    generic T (PartialOrder)
88)]
89pub fn ge(a: T, b: T) -> bool {
90    a.partial_order_ge(&b)
91}
92
93/// Determine whether `a` is greater or equal to `b`
94#[mel_treatment(
95    generic T (PartialOrder)
96    input a Stream<T>
97    input b Stream<T>
98    output is Stream<bool>
99)]
100pub async fn greater_equal() {
101    while let (Ok(a), Ok(b)) = (a.recv_one().await, b.recv_one().await) {
102        check!(is.send_one(a.partial_order_ge(&b).into()).await)
103    }
104}
105
106/// Tells whether `a` is strictly lower than `b`.
107#[mel_function(
108    generic T (PartialOrder)
109)]
110pub fn lt(a: T, b: T) -> bool {
111    a.partial_order_lt(&b)
112}
113
114/// Determine whether `a` is strictly lower than `b`
115#[mel_treatment(
116    generic T (PartialOrder)
117    input a Stream<T>
118    input b Stream<T>
119    output is Stream<bool>
120)]
121pub async fn lower_than() {
122    while let (Ok(a), Ok(b)) = (a.recv_one().await, b.recv_one().await) {
123        check!(is.send_one(a.partial_order_lt(&b).into()).await)
124    }
125}
126
127/// Tells whether `a` is lower or equal to `b`.
128#[mel_function(
129    generic T (PartialOrder)
130)]
131pub fn le(a: T, b: T) -> bool {
132    a.partial_order_le(&b)
133}
134
135/// Determine whether `a` is lower or equal to `b`
136#[mel_treatment(
137    generic T (PartialOrder)
138    input a Stream<T>
139    input b Stream<T>
140    output is Stream<bool>
141)]
142pub async fn lower_equal() {
143    while let (Ok(a), Ok(b)) = (a.recv_one().await, b.recv_one().await) {
144        check!(is.send_one(a.partial_order_le(&b).into()).await)
145    }
146}
147
148/// Gives the minimum value between `a` and `b`.
149#[mel_function(
150    generic T (Order)
151)]
152pub fn min(a: T, b: T) -> T {
153    a.order_min(&b)
154}
155
156/// Stream the minimum value between `a` and `b`.
157#[mel_treatment(
158    generic T (Order)
159    input a Stream<T>
160    input b Stream<T>
161    output min Stream<T>
162)]
163pub async fn min() {
164    while let (Ok(a), Ok(b)) = (a.recv_one().await, b.recv_one().await) {
165        check!(min.send_one(a.order_min(&b)).await)
166    }
167}
168
169/// Gives the maximum value between `a` and `b`.
170#[mel_function(
171    generic T (Order)
172)]
173pub fn max(a: T, b: T) -> T {
174    a.order_max(&b)
175}
176
177/// Stream the maximum value between `a` and `b`.
178#[mel_treatment(
179    generic T (Order)
180    input a Stream<T>
181    input b Stream<T>
182    output max Stream<T>
183)]
184pub async fn max() {
185    while let (Ok(a), Ok(b)) = (a.recv_one().await, b.recv_one().await) {
186        check!(max.send_one(a.order_max(&b)).await)
187    }
188}
189
190/// Restrain value between `min` and `max`.
191///
192/// Value is returned if it is in the [`min`,`max`] interval.
193/// If value is smaller `min` is returned, or if it greater `max` is returned.
194#[mel_function(
195    generic T (Order)
196)]
197pub fn clamp(value: T, min: T, max: T) -> T {
198    value.order_clamp(&min, &max)
199}
200
201/// Stream value restrained between `min` and `max`.
202///
203/// Value is streamed if it is in the [`min`,`max`] interval.
204/// If value is smaller `min` is sent, or if it greater `max` is sent.
205#[mel_treatment(
206    generic T (Order)
207    input value Stream<T>
208    output clamped Stream<T>
209)]
210pub async fn clamp(min: T, max: T) {
211    while let Ok(values) = value
212        .recv_many()
213        .await
214        .map(|values| Into::<VecDeque<Value>>::into(values))
215    {
216        check!(
217            clamped
218                .send_many(TransmissionValue::Other(
219                    values
220                        .into_iter()
221                        .map(|val| val.order_clamp(&min, &max))
222                        .collect()
223                ))
224                .await
225        )
226    }
227}