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(Value::Vec(vec))) = (value.recv_one().await, vec.recv_one().await) {
25        check!(
26            contains
27                .send_one(vec.iter().any(|val| val.partial_equality_eq(&value)).into())
28                .await
29        )
30    }
31}
32
33/// Concatenate `second` onto the end of `first` and return the combined vector.
34#[mel_function(
35    generic T ()
36)]
37pub fn concat(mut first: Vec<T>, mut second: Vec<T>) -> Vec<T> {
38    first.append(&mut second);
39    first
40}
41
42/// Pair-wise concatenation over two streams.
43///
44/// For each (`first`, `second`) pair received from the two streams, append `second` to `first` and emit the result through `concatened`.
45#[mel_treatment(
46    generic T ()
47    input first Stream<Vec<T>>
48    input second Stream<Vec<T>>
49    output concatened Stream<Vec<T>>
50)]
51pub async fn concat() {
52    while let (Ok(Value::Vec(mut first)), Ok(Value::Vec(mut second))) =
53        (first.recv_one().await, second.recv_one().await)
54    {
55        first.append(&mut second);
56        check!(concatened.send_one(Value::Vec(first)).await)
57    }
58}