Skip to content

Instantly share code, notes, and snippets.

@squito
squito / enum_notes.md
Last active August 29, 2015 13:56
scala enums inferior

Every so often, somebody asks why I still use Java enums instead of scala "enums". Too avoid having to search for the links every time:

  1. what is a scala "enum"? scala itself can't decide. This stack overflow question mostly summarizes the alternatives, and how come each one is incomplete. http://stackoverflow.com/questions/1321745/scala-doesnt-have-enums-what-to-use-instead-of-an-enum

if you really take some time to understand those answers, you'll see people have invested quite a bit of effort to try and mimic the behavior of java enums, and its still lacking. If you want to see even more craziness:

http://stackoverflow.com/questions/20089920/custom-scala-enum-most-elegant-version-searched

  1. On top of that, the syntax for all scala versions is weird ... I need to type more, to get less
➜ tmp javap -c -private TaggedTypes$
Compiled from "TaggedTypes.scala"
public final class TaggedTypes$ {
public static final TaggedTypes$ MODULE$;
public static {};
Code:
0: new #2 // class TaggedTypes$
3: invokespecial #12 // Method "<init>":()V
6: return
@squito
squito / binomial_confidence.R
Created June 17, 2014 14:56
Some hacky R code to explore confidence interval estimation for binomial rates, and the difference between two binomial rates.
alpha = 0.8
z <- 1.28
normal.theta.conf <- function(x, n) {
# normal approximation to a binomial. Generally agreed to be horrible
phat <- x / n
t <- z * sqrt(phat * (1 - phat)) / sqrt(n)
c(max(0,phat - t), min(1, phat + t))
}
@squito
squito / Build.scala
Created October 29, 2014 13:09
avoid sbt assemblies
...
// define your projects, with a custom "settings"
lazy val common = Project(
id = "common",
base = file("common"),
settings = baseSettings ++ Seq(
libraryDependencies ++= commonLibraryDependencies
)
)
@squito
squito / 0_reuse_code.js
Last active August 29, 2015 14:08
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@squito
squito / test-time-parse.scala
Created January 14, 2015 20:50
parsing the scalatest timing output from sbt
case class TestTime(suite: String, test: String, time: Int)
def parse(input: Iterator[String]): Seq[TestTime] = {
val TimingPattern = """(.*)\(((\d+) seconds?, )?(.*) milliseconds?\)""".r
val x = input.filter{line => line.startsWith("[info]") && !line.contains("!!! IGNORED !!!") && !line.startsWith("[info] +")}.map{_.substring("[info] ".length)}
var suiteName: String = null
var result = IndexedSeq[TestTime]()
while(x.hasNext) {
val line = x.next
if(line.endsWith(":"))
import scala.concurrent.{Future, Promise}
import scala.concurrent.duration.{Duration, FiniteDuration}
import scala.concurrent.{Await, Promise}
import scala.util.{Failure, Success}
import scala.concurrent.ExecutionContext.Implicits.global
def performSequentially[A](items: Seq[A])(f: A => Future[Unit]): Future[Unit] = {
items.headOption match {
case Some(nextItem) =>
@squito
squito / GroupedRDD.scala
Last active August 29, 2015 14:17
GroupedRDD
import java.io.{IOException, ObjectOutputStream}
import scala.language.existentials
import scala.reflect.ClassTag
import org.apache.spark._
import org.apache.spark.rdd.RDD
case class GroupedRDDPartition(
@squito
squito / CanIReadOpenDeletedFiles.scala
Created October 26, 2015 15:53
CanIReadOpenDeletedFiles.scala
import java.io._
object CanIReadOpenDeletedFile {
def main(args: Array[String]): Unit = {
try {
val f = new File("deleteme")
val out = new FileOutputStream(f)
out.write(1)
out.close()
@squito
squito / chained.py
Last active December 11, 2015 23:35
"chaining" python exception
import traceback
import sys
def a(x): b(x)
def b(x): c(x)
def c(x): d(x)