Skip to content

Instantly share code, notes, and snippets.

View BenFradet's full-sized avatar
💭
Engineering @47deg

Ben Fradet BenFradet

💭
Engineering @47deg
  • Marseille area, France
View GitHub Profile
@BenFradet
BenFradet / PredicateBooleanAlgebra.scala
Last active May 23, 2017 09:17
predicate boolean algebra
import spire.algebra._
import spire.implicits._
object Main {
def main(args: Array[String]): Unit = {
implicit def PredicateBooleanAlgebra[T] = new Bool[T => Boolean] {
def one: T => Boolean = _ => true
def zero: T => Boolean = _ => false
def and(a: T => Boolean, b: T => Boolean): T => Boolean = x => a(x) && b(x)
def or(a: T => Boolean, b: T => Boolean): T => Boolean = x => a(x) || b(x)
#!/bin/bash
# Locates the directory where the script is
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Name of our output file to submit to Kaggle
OUTPUT="classified.csv"
# Same name but it's a temp file we'll use later
TMP_FILE="${OUTPUT}2"
@BenFradet
BenFradet / spark-eb-lzo.scala
Last active February 2, 2017 13:18
Reading LZO files using elephant-bird in Spark
import com.twitter.elephantbird.mapreduce.input.MultiInputFormat
MultiInputFormat.setClassConf(classOf[Array[Byte]], hadoopConfig)
sc.newAPIHadoopFile[
org.apache.hadoop.io.LongWritable,
com.twitter.elephantbird.mapreduce.io.BinaryWritable[Array[Byte]],
MultiInputFormat[Array[Byte]]
](path)
.map(_._2.get())
@BenFradet
BenFradet / circe.scala
Created April 13, 2017 17:32
Coproduct encoder
sealed trait A
final case class B(b: Int)
final case class C(c: Int)
B(2).asJson
// { "B": {"b": 2}, "C": {"c": null} }
assemblyExcludedJars in assembly := {
val cp = (fullClasspath in assembly).value
cp filter { af =>
val file = af.data
(file.getName == "scala-library-" + scalaVersion.value + ".jar") &&
(file.getPath contains "org.scala-lang") // . instead of / for ivy
}
}
val l = List(Success(true), Success(false), Failure(new Exception("a")))
val good = l.filter { result: Validation[Exception, Boolean] =>
result match {
case Success(r) => r
case Failure(_) => false
}
}
// List(Success(true))
val bad = good.flatMap { r =>
r match {
@BenFradet
BenFradet / encoder.scala
Last active June 6, 2017 09:44
Ambiguous implicits
abstract class DT
case object NullT extends DT
case object IntegerT extends DT
case object StringT extends DT
case class ST(fields: List[(String, DT)]) extends DT
sealed trait DTEncoder[A] {
def encode: DT
// o for original t for translated
// basically a translation table
val reference = Map("schema1" -> Map("oa1" -> "ta1", "ob1" -> "tb1"),
"schema2" -> Map("oa2" -> "ta2", "ob2" -> "tb2"))
// given data with the original names and values
// v for value
val given = Map("ob2" -> "vb2", "oa1" -> "va1")
val wanted = Map("schema1" -> Map("ta1" -> "va1"), "schema2" -> Map("tb2" -> "vb2"))
// can be tweaked
val reference = Map("schema" -> Map("pr" -> "rp", "id" -> "di", "nm" -> "mn"))
val given = Map("pr12id" -> "identifier", "pr12nm" -> "name")
val wanted = Map("schema" -> Map("rp" -> "12", "di" -> "identifier", "mn" -> "name))
@BenFradet
BenFradet / review.sh
Created July 20, 2017 09:20
When applying modifications from a code review, squashing those modifications to the last commit for a particular file
#!/bin/bash
file=$1
IFS=' ' read h m <<< $(git log -n 1 --oneline $file)
git add $file
git commit -m "fixup! $m"
git rebase -i --autosquash $h^1