Categorizable trait defines logic of turning floats into hashable categorical variables. Since we only support RealNumbers for now, the idea is to treat round numbers as ordinal (or nominal if user chooses to ignore order) categories.
This commit is contained in:
@@ -0,0 +1,43 @@
|
|||||||
|
//! Traits to indicate that float variables can be viewed as categorical
|
||||||
|
//! This module assumes
|
||||||
|
|
||||||
|
pub type CategoricalFloat = u16;
|
||||||
|
|
||||||
|
// pub struct CategoricalFloat(u16);
|
||||||
|
|
||||||
|
pub trait Categorizable {
|
||||||
|
type A;
|
||||||
|
|
||||||
|
fn to_category(self) -> CategoricalFloat;
|
||||||
|
|
||||||
|
fn is_valid(self) -> bool;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Categorizable for f32 {
|
||||||
|
|
||||||
|
type A = CategoricalFloat;
|
||||||
|
|
||||||
|
fn to_category(self) -> CategoricalFloat {
|
||||||
|
self as CategoricalFloat
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_valid(self) -> bool {
|
||||||
|
let a = self.to_category();
|
||||||
|
a as f32 == self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Categorizable for f64 {
|
||||||
|
|
||||||
|
type A = CategoricalFloat;
|
||||||
|
|
||||||
|
fn to_category(self) ->CategoricalFloat {
|
||||||
|
self as CategoricalFloat
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_valid(self) -> bool {
|
||||||
|
let a = self.to_category();
|
||||||
|
a as f64 == self
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user