rs_stream 0.1.0

Immutable, transformable data streams
Documentation
/// Transformable, immutable datatype
pub struct Stream<T> {
  value: Vec<T>,
}

impl<T> Stream<T> {
  fn new(val: Vec<T>) -> Stream<T> {
    Stream { value: val }
  }
  /// Create a new stream from a vector
  pub fn from(val: Vec<T>) -> Stream<T> {
    Stream::new(val)
  }

  /// Consumes the stream and returns its value
  pub fn collect(self) -> Vec<T> {
    self.value
  }

  /// Applies a given function to every value of the stream
  pub fn map<F>(self, f: F) -> Stream<T>
  where
    F: Fn(T) -> T,
  {
    let mut return_value = Vec::new();
    for value in self.value {
      return_value.push(f(value));
    }

    Stream::new(return_value)
  }

  pub fn filter<F>(self, f: F) -> Stream<T>
  where
    F: Fn(&T) -> bool,
  {
    let mut return_value = Vec::new();
    for value in self.value {
      if f(&value) {
        return_value.push(value);
      }
    }
    Stream::new(return_value)
  }
}