standing_relations/core/op/
map.rs1use crate::core::{relation::RelationInner, Op_, Relation};
2
3pub struct FlatMap<C: Op_, I: IntoIterator, F: Fn(C::T) -> I> {
4 inner: RelationInner<C>,
5 f: F,
6}
7
8impl<C: Op_, I: IntoIterator, F: Fn(C::T) -> I> Op_ for FlatMap<C, I, F> {
9 type T = I::Item;
10
11 fn foreach<'a>(&'a mut self, mut continuation: impl FnMut(Self::T) + 'a) {
12 let FlatMap { inner, f } = self;
13 inner.foreach(|x| {
14 for y in f(x) {
15 continuation(y)
16 }
17 })
18 }
19
20 fn get_type_name() -> &'static str {
21 "flat_map"
22 }
23}
24
25impl<C: Op_> Relation<C> {
26 pub fn flat_map_<I: IntoIterator, F: Fn(C::T) -> I>(self, f: F) -> Relation<FlatMap<C, I, F>> {
27 self.context_tracker.add_relation(
28 self.dirty,
29 FlatMap {
30 inner: self.inner,
31 f,
32 },
33 vec![self.tracking_index],
34 )
35 }
36}