Skip to content

Instantly share code, notes, and snippets.

@noelwelsh
Last active May 16, 2017 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noelwelsh/09fc0af4a4ad19e4af9311bc7f05464a to your computer and use it in GitHub Desktop.
Save noelwelsh/09fc0af4a4ad19e4af9311bc7f05464a to your computer and use it in GitHub Desktop.
DataFrame
import java.util.Date
sealed trait DataFrame[A] {
import DataFrame._
def size: Int =
this.foldLeft(0)( (s, _) => s + 1 )
def foldLeft[B](z: B)(f: (B, A) => B): B =
this match {
case Apply(data) => data.foldLeft(z)(f)
case Filter(s, filter) => s.foldLeft(z){ (b, a) =>
if(filter(a)) f(b, a) else b
}
case Project(s, project) => s.foldLeft(z){ (b, a) =>
f(b, project(a))
}
}
def filter(f: A => Boolean): DataFrame[A] =
Filter(this, f)
def project[B](f: A => B): DataFrame[B] =
Project(this, f)
}
object DataFrame {
// Allows us to write DataFrame(1, 2, 3, 4)
def apply[A](as: A*): DataFrame[A] =
Apply(as.toList)
def apply[A](data: List[A]): DataFrame[A] =
Apply(data)
// DataFrame algebraic data type
final case class Apply[A](data: List[A]) extends DataFrame[A]
final case class Filter[A](source: DataFrame[A], f: A => Boolean) extends DataFrame[A]
final case class Project[A,B](source: DataFrame[A], f: A => B) extends DataFrame[B]
}
object DataFrameSyntax {
implicit class DoubleSyntax(data: DataFrame[Double]) {
def sum: Double =
data.foldLeft(0.0)(_ + _)
def average: Double =
data.sum / data.size
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment