hydroflow_lang/graph/ops/
map.rs

1use quote::quote_spanned;
2
3use super::{
4    OperatorCategory, OperatorConstraints, OperatorWriteOutput,
5    WriteContextArgs, RANGE_0, RANGE_1,
6};
7
8/// > 1 input stream, 1 output stream
9///
10/// > Arguments: A Rust closure
11///
12/// For each item passed in, apply the closure to generate an item to emit.
13///
14/// If you do not want to modify the item stream and instead only want to view
15/// each item use the [`inspect`](#inspect) operator instead.
16///
17/// > Note: The closure has access to the [`context` object](surface_flows.mdx#the-context-object).
18///
19/// ```hydroflow
20/// source_iter(vec!["hello", "world"]) -> map(|x| x.to_uppercase())
21///     -> assert_eq(["HELLO", "WORLD"]);
22/// ```
23pub const MAP: OperatorConstraints = OperatorConstraints {
24    name: "map",
25    categories: &[OperatorCategory::Map],
26    hard_range_inn: RANGE_1,
27    soft_range_inn: RANGE_1,
28    hard_range_out: RANGE_1,
29    soft_range_out: RANGE_1,
30    num_args: 1,
31    persistence_args: RANGE_0,
32    type_args: RANGE_0,
33    is_external_input: false,
34    has_singleton_output: false,
35    ports_inn: None,
36    ports_out: None,
37    input_delaytype_fn: |_| None,
38    write_fn: |&WriteContextArgs {
39                   root,
40                   op_span,
41                   ident,
42                   inputs,
43                   outputs,
44                   is_pull,
45                   arguments,
46                   ..
47               },
48               _| {
49        let func = &arguments[0];
50        let write_iterator = if is_pull {
51            let input = &inputs[0];
52            quote_spanned! {op_span=>
53                #[allow(clippy::map_clone, reason = "hydroflow has no explicit `cloned`/`copied` operator")]
54                let #ident = #input.map(#func);
55            }
56        } else {
57            let output = &outputs[0];
58            quote_spanned! {op_span=>
59                let #ident = #root::pusherator::map::Map::new(#func, #output);
60            }
61        };
62        Ok(OperatorWriteOutput {
63            write_iterator,
64            ..Default::default()
65        })
66    },
67};