Trait yata::core::Method [−][src]
pub trait Method: Debug {
type Params;
type Input: ?Sized;
type Output;
fn new(
parameters: Self::Params,
initial_value: &Self::Input
) -> Result<Self, Error>
where
Self: Sized;
fn next(&mut self, value: &Self::Input) -> Self::Output;
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.
Associated Types
Required methods
Static method for creating an instance of the method with given parameters
and initial value
(simply first input value)
Provided methods
Returns memory size of the method (size, align)
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());
Applies method to the sequence in-place.
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.
Creates new Method
instance and applies it to the sequence
.
Creates a function from the Method
instance