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