Skip to content

Commit

Permalink
Merge pull request #68 from andrewcsmith/add-signal-map-zip-map
Browse files Browse the repository at this point in the history
Issue #63: Add signal funcs Map and ZipMap
  • Loading branch information
mitchmindtree authored Sep 7, 2017
2 parents 45702f1 + ad87c16 commit 2c661f9
Showing 1 changed file with 107 additions and 0 deletions.
107 changes: 107 additions & 0 deletions src/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,23 @@ pub struct GenMut<G, F> {
frame: core::marker::PhantomData<F>,
}

/// A signal that maps from one signal to another
#[derive(Clone)]
pub struct Map<M, S, F> {
map: M,
signal: S,
frames: core::marker::PhantomData<F>,
}

/// A signal that iterates two signals in parallel and combines them with a function
#[derive(Clone)]
pub struct ZipMap<M, S, O, F> {
map: M,
this: S,
other: O,
frame: core::marker::PhantomData<F>
}

/// A type that wraps an Iterator and provides a `Signal` implementation for it.
#[derive(Clone)]
pub struct FromIterator<I> {
Expand Down Expand Up @@ -824,6 +841,69 @@ pub fn gen_mut<G, F>(gen_mut: G) -> GenMut<G, F>
}


/// A signal that maps one set of frames to another
///
/// # Example
///
/// ```rust
/// extern crate sample;
///
/// use sample::{signal, Signal};
///
/// fn main() {
/// let frames = signal::gen(|| [0.5]);
/// let mut mapper = signal::map(frames, |f| [f[0], 0.25]);
/// assert_eq!(mapper.next(), [0.5, 0.25]);
/// assert_eq!(mapper.next(), [0.5, 0.25]);
/// assert_eq!(mapper.next(), [0.5, 0.25]);
/// }
/// ```
pub fn map<M, S, F>(signal: S, map: M) -> Map<M, S, F>
where M: FnMut(S::Frame) -> F,
S: Signal,
F: Frame,
{
Map {
map: map,
signal: signal,
frames: core::marker::PhantomData,
}
}


/// A signal that maps one set of frames to another
///
/// # Example
///
/// ```rust
/// extern crate sample;
///
/// use sample::{signal, Signal};
///
/// fn main() {
/// let frames = signal::gen(|| [0.5]);
/// let more_frames = signal::gen(|| [0.25]);
/// let mut mapper = signal::zip_map(frames, more_frames, |f, o| [f[0], o[0]]);
/// assert_eq!(mapper.next(), [0.5, 0.25]);
/// assert_eq!(mapper.next(), [0.5, 0.25]);
/// assert_eq!(mapper.next(), [0.5, 0.25]);
/// }
/// ```
pub fn zip_map<M, S, O, F>(this: S, other: O, map: M) -> ZipMap<M, S, O, F>
where M: FnMut(S::Frame, O::Frame) -> F,
S: Signal,
O: Signal,
F: Frame,
{
ZipMap {
map: map,
this: this,
other: other,
frame: core::marker::PhantomData,
}
}


/// Create a new `Signal` from the given `Frame`-yielding `Iterator`.
///
/// When the `Iterator` is exhausted, the new `Signal` will yield `F::equilibrium`.
Expand Down Expand Up @@ -1118,6 +1198,33 @@ impl<G, F> Signal for GenMut<G, F>
}


impl<M, S, F> Signal for Map<M, S, F>
where M: FnMut(S::Frame) -> F,
S: Signal,
F: Frame,
{
type Frame = F;
#[inline]
fn next(&mut self) -> Self::Frame {
(self.map)(self.signal.next())
}
}


impl<M, S, O, F> Signal for ZipMap<M, S, O, F>
where M: FnMut(S::Frame, O::Frame) -> F,
S: Signal,
O: Signal,
F: Frame,
{
type Frame = F;
#[inline]
fn next(&mut self) -> Self::Frame {
(self.map)(self.this.next(), self.other.next())
}
}


impl<S> Signal for Hz<S>
where S: Signal<Frame=[f64; 1]>,
{
Expand Down

0 comments on commit 2c661f9

Please sign in to comment.