Created
September 27, 2020 09:10
-
-
Save pjankiewicz/0efd00c39c3614b113dac78cafc971a4 to your computer and use it in GitHub Desktop.
Using symbols to refer to structs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![allow(dead_code)] | |
use std::collections::HashMap; | |
#[derive(PartialEq)] | |
struct Click { | |
item_id: String, | |
} | |
#[derive(PartialEq)] | |
struct View { | |
item_id: String, | |
} | |
#[derive(PartialEq)] | |
struct Buy { | |
item_id: String, | |
} | |
#[derive(PartialEq, Copy, Clone)] | |
enum EventTypeSymbol { | |
Click, | |
View, | |
Buy, | |
} | |
#[derive(PartialEq)] | |
enum EventType { | |
Click(Click), | |
View(View), | |
Buy(Buy), | |
} | |
struct AccumulatorContext { | |
item_id: Option<String>, | |
user_id: Option<String> | |
} | |
// use Event enum | |
struct XTR { | |
nom: EventTypeSymbol, | |
denom: EventTypeSymbol, | |
nom_values: HashMap<String, i32>, | |
denom_values: HashMap<String, i32>, | |
} | |
impl XTR { | |
fn new(nom: EventTypeSymbol, denom: EventTypeSymbol) -> Self { | |
Self { | |
nom, | |
denom, | |
nom_values: HashMap::new(), | |
denom_values: HashMap::new(), | |
} | |
} | |
fn inc_nom(&mut self, key: &str) { | |
*self.nom_values.entry(key.to_string()).or_insert(0) += 1; | |
} | |
fn inc_denom(&mut self, key: &str) { | |
*self.denom_values.entry(key.to_string()).or_insert(0) += 1; | |
} | |
fn update(&mut self, event: &EventType) -> f32 { | |
match (event, self.nom) { | |
// Match nominator | |
(EventType::Buy(buy), EventTypeSymbol::Buy) => self.inc_nom(&buy.item_id), | |
(EventType::View(view), EventTypeSymbol::View) => self.inc_nom(&view.item_id), | |
(EventType::Click(click), EventTypeSymbol::Click) => self.inc_nom(&click.item_id), | |
_ => (), | |
} | |
match (event, self.denom) { | |
// Match denominator | |
(EventType::Buy(buy), EventTypeSymbol::Buy) => self.inc_denom(&buy.item_id), | |
(EventType::View(view), EventTypeSymbol::View) => self.inc_denom(&view.item_id), | |
(EventType::Click(click), EventTypeSymbol::Click) => self.inc_denom(&click.item_id), | |
_ => (), | |
}; | |
1.0 | |
} | |
fn get(self, context: &AccumulatorContext) -> Option<f64> { | |
match &context.item_id { | |
Some(item_id) => { | |
let nom = (*self.nom_values.get(item_id).unwrap_or(&0)) as f64; | |
let denom = (*self.denom_values.get(item_id).unwrap_or(&1)) as f64; | |
Some(nom / denom) | |
} | |
_ => None | |
} | |
} | |
} | |
fn main() { | |
let mut ctr = XTR::new(EventTypeSymbol::Buy, EventTypeSymbol::View); | |
for _ in 0..100000 { | |
let event_view = EventType::View(View { | |
item_id: "item_1".to_string(), | |
}); | |
ctr.update(&event_view); | |
} | |
for _ in 0..10000 { | |
let event_buy = EventType::Buy(Buy { | |
item_id: "item_1".to_string(), | |
}); | |
ctr.update(&event_buy); | |
} | |
println!("{:?}", ctr.nom_values); | |
println!("{:?}", ctr.denom_values); | |
let context = AccumulatorContext{item_id: Some("item_1".to_string()), user_id: None}; | |
println!("{:?}", ctr.get(&context)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment