Skip to content

Commit 800515f

Browse files
authored
feat: add EffectOut::map_effect (#88)
EffectOut is a functor, and w/ and_extend, it is also sometimes a monad. There's nothing stopping it from being a bifunctor, except for the lack of the second mapping function. This change adds such a function `map_effect`.
1 parent 48dc336 commit 800515f

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

src/effect_out.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl<E, O> EffectOut<E, O>
2020
where
2121
E: Effect,
2222
{
23-
/// Maps a `EffectOut<E, O>` to an `EffectOut<E, O2>` by applying a function to the `output`.
23+
/// Maps a `EffectOut<E, O>` to an `EffectOut<E, O2>` by applying a function to the `out`.
2424
///
2525
/// # Examples
2626
/// ```
@@ -44,6 +44,37 @@ where
4444
}
4545
}
4646

47+
/// Maps a `EffectOut<E, O>` to an `EffectOut<E2, O>` by applying a function to the `effect`.
48+
///
49+
/// # Examples
50+
/// ```
51+
/// # #[cfg(feature = "derive")] {
52+
/// use bevy::prelude::*;
53+
/// use bevy_pipe_affect::prelude::*;
54+
///
55+
/// #[derive(Debug, PartialEq, Eq, Message)]
56+
/// struct MyMessage<T>(T);
57+
///
58+
/// let initial = effect_out(message_write(MyMessage(4)), 5);
59+
/// let mapped = initial.map_effect(|m| message_write(MyMessage(format!("{}", m.message.0))));
60+
///
61+
/// assert_eq!(
62+
/// mapped,
63+
/// effect_out(message_write(MyMessage("4".to_string())), 5)
64+
/// );
65+
/// # }
66+
/// ```
67+
pub fn map_effect<E2>(self, f: impl FnOnce(E) -> E2) -> EffectOut<E2, O>
68+
where
69+
E2: Effect,
70+
{
71+
let EffectOut { effect, out } = self;
72+
EffectOut {
73+
effect: f(effect),
74+
out,
75+
}
76+
}
77+
4778
/// Apply a function `f` to the `output` and return an `EffectOut` with [`Effect`] combination.
4879
///
4980
/// i.e. `f` takes `output: O` and returns an [`Effect`] (or `EffectOut`). Then, this returns

0 commit comments

Comments
 (0)