pub trait Method {
type Params;
type Input: ?Sized;
type Output;
// Required methods
fn new(
parameters: Self::Params,
initial_value: &Self::Input,
) -> Result<Self, Error>
where Self: Sized;
fn next(&mut self, value: &Self::Input) -> Self::Output;
// Provided methods
fn with_history(
parameters: Self::Params,
initial_value: &Self::Input,
) -> Result<WithHistory<Self, Self::Output>, Error>
where Self: Sized,
Self::Output: Debug + Clone { ... }
fn with_last_value(
parameters: Self::Params,
initial_value: &Self::Input,
) -> Result<WithLastValue<Self, Self::Output>, Error>
where Self: Sized,
Self::Output: Debug + Clone { ... }
fn name(&self) -> &str { ... }
fn memsize(&self) -> (usize, usize)
where Self: Sized { ... }
fn over<S>(&mut self, inputs: S) -> Vec<Self::Output>
where S: Sequence<Self::Input>,
Self::Input: Sized,
Self: Sized { ... }
fn apply<T, S>(&mut self, sequence: &mut S)
where S: Sequence<T> + AsMut<[T]> + ?Sized,
Self: Method<Input = T, Output = T> + Sized { ... }
fn new_over<S>(
parameters: Self::Params,
inputs: S,
) -> Result<Vec<Self::Output>, Error>
where S: Sequence<Self::Input>,
Self::Input: Sized,
Self: Sized { ... }
fn new_apply<T, S>(
parameters: Self::Params,
sequence: &mut S,
) -> Result<(), Error>
where S: Sequence<T> + AsMut<[T]>,
Self: Method<Input = T, Output = T> + Sized { ... }
fn into_fn<'a>(
self,
) -> Box<dyn FnMut(&'a <Self as Method>::Input) -> <Self as Method>::Output>
where Self: Sized + 'static,
Self::Input: 'static { ... }
fn new_fn(
params: Self::Params,
initial_value: &Self::Input,
) -> Result<Box<dyn FnMut(&'_ <Self as Method>::Input) -> <Self as Method>::Output>, Error>
where Self: Sized + 'static { ... }
}Expand description
Trait for creating methods for timeseries
§Regular methods usage
§Iterate over vector’s values
use yata::methods::SMA;
use yata::prelude::*;
let s: Vec<_> = vec![1.,2.,3.,4.,5.,6.,7.,8.,9.,10.];
let mut ma = SMA::new(2, &s[0]).unwrap();
s.iter().enumerate().for_each(|(index, value)| {
assert_eq!(ma.next(value), (*value + s[index.saturating_sub(1)])/2.);
});§Get a whole new vector over the input vector
You can call method over any Sequence:
use yata::methods::SMA;
use yata::prelude::*;
let s: Vec<_> = vec![1.,2.,3.,4.,5.,6.,7.,8.,9.,10.];
let mut ma = SMA::new(2, &s[0]).unwrap();
let result = ma.over(s);
assert_eq!(result.as_slice(), &[1., 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5]);Or you can provide Method to Sequence:
use yata::methods::SMA;
use yata::prelude::*;
let s: Vec<_> = vec![1.,2.,3.,4.,5.,6.,7.,8.,9.,10.];
let mut ma = SMA::new(2, &s[0]).unwrap();
let result = s.call(&mut ma);
assert_eq!(result.as_slice(), &[1., 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5]);Or you can even change Sequence values in-place:
use yata::methods::SMA;
use yata::prelude::*;
let mut s: Vec<_> = vec![1.,2.,3.,4.,5.,6.,7.,8.,9.,10.];
let mut ma = SMA::new(2, &s[0]).unwrap();
s.apply(&mut ma);
assert_eq!(s.as_slice(), &[1., 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5]);§Be advised
There is no reset method on the trait. If you need reset a state of the Method instance, you should just create a new one.
Required Associated Types§
Required Methods§
Provided Methods§
Sourcefn with_history(
parameters: Self::Params,
initial_value: &Self::Input,
) -> Result<WithHistory<Self, Self::Output>, Error>
fn with_history( parameters: Self::Params, initial_value: &Self::Input, ) -> Result<WithHistory<Self, Self::Output>, Error>
Creates an instance of the method with given parameters and initial value, wrapped by historical data holder
Sourcefn with_last_value(
parameters: Self::Params,
initial_value: &Self::Input,
) -> Result<WithLastValue<Self, Self::Output>, Error>
fn with_last_value( parameters: Self::Params, initial_value: &Self::Input, ) -> Result<WithLastValue<Self, Self::Output>, Error>
Creates an instance of the method with given parameters and initial value, wrapped by last produced value holder
Sourcefn memsize(&self) -> (usize, usize)where
Self: Sized,
👎Deprecated
fn memsize(&self) -> (usize, usize)where
Self: Sized,
Returns memory size of the method (size, align)
Sourcefn over<S>(&mut self, inputs: S) -> Vec<Self::Output>
fn over<S>(&mut self, inputs: S) -> Vec<Self::Output>
Iterates the Method over the given inputs slice and returns Vec of output values.
§Guarantees
The length of an output Vec is always equal to the length of an inputs slice.
use yata::methods::SMA;
use yata::prelude::*;
let s: Vec<_> = vec![1.,2.,3.,4.,5.,6.,7.,8.,9.,10.];
let mut ma = SMA::new(5, &s[0]).unwrap();
let result = ma.over(&s);
assert_eq!(result.len(), s.len());use yata::methods::SMA;
use yata::prelude::*;
let s: Vec<_> = vec![1.,2.,3.,4.,5.,6.,7.,8.,9.,10.];
let mut ma = SMA::new(100, &s[0]).unwrap();
let result = ma.over(&s);
assert_eq!(result.len(), s.len());Sourcefn new_over<S>(
parameters: Self::Params,
inputs: S,
) -> Result<Vec<Self::Output>, Error>
fn new_over<S>( parameters: Self::Params, inputs: S, ) -> Result<Vec<Self::Output>, Error>
Creates new Method instance and iterates it over the given inputs slice and returns Vec of output values.
§Guarantees
The length of an output Vec is always equal to the length of an inputs slice.
Sourcefn new_apply<T, S>(
parameters: Self::Params,
sequence: &mut S,
) -> Result<(), Error>
fn new_apply<T, S>( parameters: Self::Params, sequence: &mut S, ) -> Result<(), Error>
Creates new Method instance and applies it to the sequence.