Skip to content

Instantly share code, notes, and snippets.

@logicalguess
logicalguess / ProportionStats.scala
Last active June 23, 2016 01:01
p-values and confidence intervals for proportions
import org.apache.commons.math3.distribution.{ChiSquaredDistribution, NormalDistribution}
import scala.math._
/**
* Created by logicalguess on 6/22/16.
*/
case class ProportionInfo(attempts: Int, successes: Int) {
val failures: Double = attempts - successes
@logicalguess
logicalguess / SequentialReceiver
Created September 28, 2016 18:12
Spark Receiver
import org.apache.spark.SparkConf;
import org.apache.spark.serializer.KryoSerializer;
import org.apache.spark.storage.StorageLevel;
import org.apache.spark.streaming.Duration;
import org.apache.spark.streaming.Durations;
import org.apache.spark.streaming.api.java.JavaReceiverInputDStream;
import org.apache.spark.streaming.api.java.JavaStreamingContext;
import org.apache.spark.streaming.receiver.Receiver;
@logicalguess
logicalguess / Dag.scala
Created October 18, 2016 18:29 — forked from xuwei-k/Dag.scala
directed acyclic graph
package scalaz
final case class Dag[A](value: A, dependencies: IList[Dag[A]]){
def toTree(implicit A: Order[A]): DagTree[A] =
DagTree(value, dependencies.map(_.toTree))
}
object Dag {
def single[A](a: A): Dag[A] = Dag(a, INil())
package org.apache.beam.examples;
import org.apache.beam.sdk.coders.Coder;
import org.apache.beam.sdk.coders.SerializableCoder;
import org.apache.beam.sdk.coders.StringUtf8Coder;
import org.apache.beam.sdk.io.Read;
import org.apache.beam.sdk.io.UnboundedSource;
import org.apache.beam.sdk.options.PipelineOptions;
import org.apache.beam.sdk.testing.TestPipeline;
import org.apache.beam.sdk.transforms.MapElements;
val sum: (Int, Int, Int) => Int = _ + _ + _
val sumCurried: Int => Int => Int => Int = sum.curried
val f: Int => Int = sum.curried(1)(2)
val g: Int => Int = sum(1, 2, _: Int)
println(f(3)) //6
println(g(3)) //6
@logicalguess
logicalguess / reflection.scala
Last active August 22, 2017 05:13
scala reflection
object fs {
def comb(list: Any*): String = {
list.mkString
}
def comb1(i: Int, s1: String, s2: String): String = i + s1 + s2
}
val args = List(2, "b", "c")
@logicalguess
logicalguess / ShapelessFunctions.scala
Last active September 9, 2017 15:55
combining functions into a PartialFunction
package logicalguess
import shapeless.HList._
import shapeless._
import scala.annotation.implicitNotFound
import scala.language.implicitConversions
import scala.reflect.ClassTag
object ShapelessFunctions {
@logicalguess
logicalguess / HListMapper.scala
Last active September 11, 2017 02:49
mapper from an HList to another
trait HListMapper[In <: HList, Out <: HList] {
def apply(in: In): Out
}
object HListMapper {
implicit def caseHNil: HListMapper[HNil, HNil] = (in: HNil) => HNil
implicit def caseHList[H1, H2, T1 <: HList, T2 <: HList]
(
@logicalguess
logicalguess / HListFolder.scala
Last active September 11, 2017 04:21
e.g. combining an HList of functions into a partial function
package logicalguess
import shapeless.{::, HList, HNil}
trait HListFolder[In <: HList, Out] {
def apply(in: In): Out
}
object HListFolder {
@logicalguess
logicalguess / Call.scala
Last active September 21, 2017 01:57
type safe partial function union/merge with compile time verification
package logicalguess.receiver
import scala.reflect.ClassTag
case class Call[U <: Union](pf: PartialFunction[Any, Any]) {
import Call._
def union[In](f: Function[In, Any])(implicit ct: ClassTag[In]) = Call[U | In](pf.orElse(downcast(f)))