lfa/basis/
closure.rs

1use super::*;
2use crate::{Features, Result};
3use spaces::{Card, Dim, Space};
4
5#[derive(Clone, Debug, PartialEq)]
6#[cfg_attr(
7    feature = "serde",
8    derive(Serialize, Deserialize),
9    serde(crate = "serde_crate")
10)]
11pub struct Closure<F> {
12    n_features: usize,
13    mapping: F,
14}
15
16impl<F> Closure<F> {
17    pub fn new(n_features: usize, mapping: F) -> Self {
18        Closure {
19            n_features,
20            mapping,
21        }
22    }
23}
24
25impl<F> Space for Closure<F> {
26    type Value = Features;
27
28    fn dim(&self) -> Dim { Dim::Finite(self.n_features) }
29
30    fn card(&self) -> Card { Card::Infinite }
31}
32
33impl<T, F> Basis<T> for Closure<F>
34where F: Fn(T) -> Result<Features>
35{
36    fn project(&self, input: T) -> Result<Features> { (self.mapping)(input) }
37}
38
39impl<F> Combinators for Closure<F> {}