Skip to content

Instantly share code, notes, and snippets.

@leifwickland
leifwickland / example.js
Last active April 11, 2023 21:21
Floating point addition is not associative
4.5e15 + (2e-1 + 2e-1) != (4.5e15 + 2e-1) + 2e-1
import com.rubiconproject.dt.silvergate.sparktest.SharedSparkSession
import com.rubiconproject.science.util.SparkImplicitsWithoutSqlContext._
import org.specs2.mutable.Specification
import scala.util.Random
import ExampleRollupTest._
class ExampleRollupTest extends Specification with SharedSparkSession {
"a" should {
"b" in {
@leifwickland
leifwickland / encodePair.scala
Last active October 13, 2019 17:44
encodePair.scala
def encodePair(pair: (Char, Char)): (Char, Char) = findLetterInSquare(pair._1) match {
case (a, b) => findLetterInSquare(pair._2) match {
case (`a`, `b`) => throw new Exception("UNPOSSIBLE!") // same cell
case (`a`, d ) => (indexToLetter((a, (b+1)%5)), indexToLetter((a, (d+1)%5))) // same row
case ( c, `b`) => (indexToLetter(((a + 1)%5, b)), indexToLetter(((c+1)%5, b))) // same column
case ( c, d ) => (indexToLetter((a, d)),indexToLetter((c, b)))
}
}
@leifwickland
leifwickland / ListOps.scala
Created December 10, 2018 20:23
distinctby
final class ListOps[A](val l: List[A]) extends AnyVal {
def distinctBy[K](f: A => K): List[A] = {
val seen = collection.mutable.HashSet.empty[K]
@tailrec
def loop(l: List[A], distinctAccum: List[A]): List[A] = l match {
case Nil => distinctAccum
case t :: ts =>
val k = f(t)
if (seen.contains(k)) loop(ts, distinctAccum)
@leifwickland
leifwickland / ProperPercentage.scala
Created November 7, 2018 17:12
Refined percentage
/// A double floating point number which allows values in the range [0.0,100.0].
object ProperPercentage {
type L = W.`0.0`.T
type H = W.`100.0`.T
type P = Interval.Closed[L, H]
type N = Double
type T = N Refined P
val gen: Gen[Refined[N, Closed[L, H]]] = intervalClosedArbitrary[Refined, N, L, H].arbitrary
def safely(d: Double): String \/ T = refineV[P].apply(d).fold(_.left, _.right)
def unsafe(d: Double): T = safely(d).valueOr(m => throw new Exception(m))
@leifwickland
leifwickland / AdtFormat.scala
Created October 19, 2018 19:36
Creates a Play JSON Format instance for sealed families (ADTs) with `case class`es and optionally `case object`s
import play.api.libs.json._
import scalaz.NonEmptyList
import scala.reflect.ClassTag
import scala.reflect.runtime.universe.TypeTag
/**
* Provides a Play JSON Format for an ADT with case classes and possibly case object members.
*
* Heavily inspired by https://gist.github.com/realpeterz/aecb53a67fb723485eb66d544a67d580
@leifwickland
leifwickland / IndexedSeqSyntax.scala
Created August 8, 2018 20:34
scanFilter/scanFind
object IndexedSeqSyntax {
@specialized(Specializable.Bits32AndUp)
implicit class IndexedSeqOps[T](ts: IndexedSeq[T]) {
/**
* Accumulates values, S, derived from T, and returns the first T, if any when p is true for S.
*
* It's kind of like a lazy conjunction of the standard library's `scan` and `find`.
*
* It could be implemented less efficiently as:
object QueryUtil {
/**
* Generates an SQL comment that indicates where in Scala source the query originated.
*/
def locationFragment(implicit file: sourcecode.File, line: sourcecode.Line): Fragment = {
Fragment.const(s"/* Query from ${file.value}:${line.value} */\n")
}
}
@leifwickland
leifwickland / Version.scala
Created April 18, 2017 17:52
Retrieves the version information which is stored in the manifest of a JAR
/**
* Reads the version number out of the manifest.
*/
object Version {
def apply: Option[String] = {
val myClass = this.getClass
val classFileName = s"${myClass.getSimpleName}.class"
val myUrl = myClass.getResource(classFileName)
val myUrlString = myUrl.toString
val lastBang = myUrlString.lastIndexOf('!')
@leifwickland
leifwickland / ReducingIterator.scala
Last active January 19, 2017 23:54
ReducingIterator.scala
class ReducingIterator[A] private (iterator: Iterator[A], shouldGroup: (A, A) => Boolean, reduce: (A, A) => A) extends Iterator[A] {
// Basic implementation robbed from https://github.com/fulcrumgenomics/fgbio/blob/27b0bc20f8baf26f555bb056073bb8de528c4ef3/src/main/scala/com/fulcrumgenomics/util/BetterBufferedIterator.scala#L36
private var buffer: Option[A] = maybeNext
/** Returns a Some(A) if there is a next in the iterator else None. */
private def maybeNext = if (this.iterator.hasNext) Some(this.iterator.next()) else None
/** True if head() and hasNext() will return an item, false otherwise. */
override def hasNext: Boolean = this.buffer.nonEmpty