Customising an existing data structure

The key idea behind the library allows what are, to my mind some exciting extensions.

It would be very common, to have your own representations of data. An example of a library with it's own data structures (there are many) might breeze, which is a library for linear algebra in scala.

The below extension method would add line plotting functionality, to all Breeze DenseVectors. What's cool? You don't need to own either Breeze or dedav to make this work.

extension [T: Numeric](l: DenseVector[T])(using plotTarget: PlotTarget)
  def plotLineChart(mods: JsonMod = List()): LineChart =

Give it a go in a project - this turns a breeze vector into a plottable data structure that emits a line chart!

The moment where I figured it might be worth an attempt at at publishing this library was this;

import breeze.stats.distributions._
import Rand.VariableSeed._
import viz.PlotTarget
import viz.vega.plots.ProbabilityDensity

extension (l: HasInverseCdf)(using plotTarget: PlotTarget)
    def plotDensity(mods: Seq[ujson.Value => Unit] = List()): ProbabilityDensity =
        val probs = breeze.linalg.linspace(0.0+1.0/1000.0,1.0-1.0/1000.0,1000)
        val icdfs =
        val inject = for((i, p) <- icdfs) yield {
            ujson.Obj("u" -> i, "v" -> p)
        val pipeData :ujson.Value => Unit = spec => spec("data")(0) = ujson.Obj("name" -> "points", "values"->inject)
        ProbabilityDensity(List(pipeData, viz.Utils.fillDiv))

We extend the trait HasInverseCdf, and now? Every distribution with an inverse CDF is plottable!

in 12. lines. of. code. It took way longer to write the documentation, than implement that entire class of plots :-).

// res0: ProbabilityDensity = ProbabilityDensity(
//   mods = List(
//     repl.MdocSession$MdocApp$$Lambda$15234/1845973390@10c6e83d,
//     viz.Utils$$$Lambda$15217/1662401320@7effd624
//   )
// )
// res1: ProbabilityDensity = ProbabilityDensity(
//   mods = List(
//     repl.MdocSession$MdocApp$$Lambda$15234/1845973390@24bbbb67,
//     viz.Utils$$$Lambda$15217/1662401320@7effd624
//   )
// )