Skip to main content

std_mel/ops/vec/
mod.rs

1use melodium_core::*;
2use melodium_macro::{check, mel_function, mel_treatment};
3
4pub mod block;
5
6/// Return `true` if `vector` contains a value equal to `value`.
7#[mel_function(
8    generic T (PartialEquality)
9)]
10pub fn contains(vector: Vec<T>, value: T) -> bool {
11    vector.iter().any(|val| val.partial_equality_eq(&value))
12}
13
14/// Pair-wise membership check over two streams.
15///
16/// For each (`value`, `vec`) pair received from the two streams, emit `true` through `contains` if `vec` contains `value`.
17#[mel_treatment(
18    generic T (PartialEquality)
19    input value Stream<T>
20    input vec Stream<Vec<T>>
21    output contains Stream<bool>
22)]
23pub async fn contains() {
24    while let (Ok(value), Ok(vec_value)) = (value.recv_one().await, vec.recv_one().await) {
25        let vec = match vec_value {
26            Value::Vec(vec) => vec,
27            Value::Packed(arr) => arr.into_values(),
28            _ => break,
29        };
30        check!(
31            contains
32                .send_one_as(vec.iter().any(|val| val.partial_equality_eq(&value)))
33                .await
34        )
35    }
36}
37
38/// Concatenate `second` onto the end of `first` and return the combined vector.
39#[mel_function(
40    generic T ()
41)]
42pub fn concat(mut first: Vec<T>, mut second: Vec<T>) -> Vec<T> {
43    first.append(&mut second);
44    first
45}
46
47/// Pair-wise concatenation over two streams.
48///
49/// For each (`first`, `second`) pair received from the two streams, append `second` to `first` and emit the result through `concatened`.
50#[mel_treatment(
51    generic T ()
52    input first Stream<Vec<T>>
53    input second Stream<Vec<T>>
54    output concatened Stream<Vec<T>>
55)]
56pub async fn concat() {
57    while let (Ok(first_value), Ok(second_value)) =
58        (first.recv_one().await, second.recv_one().await)
59    {
60        let mut first = match first_value {
61            Value::Vec(vec) => vec,
62            Value::Packed(arr) => arr.into_values(),
63            _ => break,
64        };
65        let mut second = match second_value {
66            Value::Vec(vec) => vec,
67            Value::Packed(arr) => arr.into_values(),
68            _ => break,
69        };
70        first.append(&mut second);
71        check!(concatened.send_one_as(first).await)
72    }
73}