Skip to content

Commit

Permalink
DisplaySlice moved to display mod
Browse files Browse the repository at this point in the history
  • Loading branch information
ekoutanov committed Oct 17, 2023
1 parent 13b6b3f commit 9747300
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 41 deletions.
3 changes: 2 additions & 1 deletion examples/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use stanza::renderer::Renderer;

use bentobox::linear::Matrix;
use bentobox::{mc, overround};
use bentobox::display::DisplaySlice;
use bentobox::mc::DilatedProbs;
use bentobox::print::{DerivedPrice, DisplaySlice, tabulate};
use bentobox::print::{DerivedPrice, tabulate};
use bentobox::probs::SliceExt;
use bentobox::selection::{Rank, Runner};

Expand Down
3 changes: 2 additions & 1 deletion src/bin/prices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ use bentobox::data::{download_by_id, read_from_file, EventDetailExt, RaceSummary
use bentobox::linear::Matrix;
use bentobox::mc::DilatedProbs;
use bentobox::opt::{gd, GradientDescentConfig, GradientDescentOutcome};
use bentobox::print::{tabulate, DerivedPrice, DisplaySlice};
use bentobox::print::{tabulate, DerivedPrice};
use bentobox::probs::{MarketPrice, SliceExt};
use bentobox::selection::{Rank, Runner, Selection, Selections};
use bentobox::{mc, overround};
use bentobox::display::DisplaySlice;

const MC_ITERATIONS_TRAIN: u64 = 100_000;
const MC_ITERATIONS_EVAL: u64 = 1_000_000;
Expand Down
39 changes: 39 additions & 0 deletions src/display.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use std::fmt::{Display, Formatter};

pub struct DisplaySlice<'a, D: Display> {
items: &'a [D]
}
impl<'a, D: Display> Display for DisplaySlice<'a, D> where D: Display {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "[")?;
let len = self.items.len();
for (index, item) in self.items.iter().enumerate() {
write!(f, "{item}")?;
if index != len - 1 {
write!(f, ", ")?;
}
}
write!(f, "]")?;
Ok(())
}
}

impl<'a, D: Display> From<&'a [D]> for DisplaySlice<'a, D> {
fn from(items: &'a [D]) -> Self {
DisplaySlice { items }
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn display_slice() {
let data = vec![4, 5, 6, 8];
assert_eq!("[4, 5, 6, 8]", format!("{}", DisplaySlice::from(&*data)));

let data: Vec<usize> = vec![];
assert_eq!("[]", format!("{}", DisplaySlice::from(&*data)));
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

pub mod capture;
pub mod data;
pub mod display;
pub mod linear;
pub mod mc;
pub mod opt;
Expand Down
39 changes: 0 additions & 39 deletions src/print.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::fmt::{Display, Formatter};
use stanza::style::{HAlign, Header, MinWidth, Separator, Styles};
use stanza::table::{Col, Row, Table};
use crate::linear::Matrix;
Expand All @@ -18,30 +17,6 @@ impl MarketPrice for DerivedPrice {
}
}

pub struct DisplaySlice<'a, D: Display> {
items: &'a [D]
}
impl<'a, D: Display> Display for DisplaySlice<'a, D> where D: Display {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "[")?;
let len = self.items.len();
for (index, item) in self.items.iter().enumerate() {
write!(f, "{item}")?;
if index != len - 1 {
write!(f, ", ")?;
}
}
write!(f, "]")?;
Ok(())
}
}

impl<'a, D: Display> From<&'a [D]> for DisplaySlice<'a, D> {
fn from(items: &'a [D]) -> Self {
DisplaySlice { items }
}
}

pub fn tabulate(derived: &Matrix<DerivedPrice>) -> Table {
let mut table = Table::default()
.with_cols({
Expand Down Expand Up @@ -130,18 +105,4 @@ pub fn tabulate(derived: &Matrix<DerivedPrice>) -> Table {
}

table
}

#[cfg(test)]
mod tests {
use crate::print::DisplaySlice;

#[test]
fn display_slice() {
let data = vec![4, 5, 6, 8];
assert_eq!("[4, 5, 6, 8]", format!("{}", DisplaySlice::from(&*data)));

let data: Vec<usize> = vec![];
assert_eq!("[]", format!("{}", DisplaySlice::from(&*data)));
}
}

0 comments on commit 9747300

Please sign in to comment.