Skip to content

Instantly share code, notes, and snippets.

View johnynek's full-sized avatar

P. Oscar Boykin johnynek

View GitHub Profile
@johnynek
johnynek / gist:4472451
Last active December 10, 2015 18:08
ExceptionalBijection
def orElse[A,B](fna: A => Option[B])(fni: B => Option[A])(implicit bi: Bijection[A,B]): Bijection[A,B] =
new Bijection[A,B] {
def apply(a: A) = fna(a).getOrElse(bij(a))
override invert(b: B) = fni(b).getOrElse(bij.invert(b))
}
@johnynek
johnynek / innerclass-bug.scala
Last active December 10, 2015 20:28
kryo scala inner-class bug
trait AwesomeFns {
val myfun = { x: Int => 2*x }
}
object BaseFns extends AwesomeFns {
val myfun2 = { x: Int => 4*x }
def apply(x: Int) = myfun.apply(x)
}
// Trying to serialize myfun2 with Kryo fields serializer works, but not myfun.
import org.scalacheck.Arbitrary
import org.scalacheck.Prop.forAll
object GraphTest extends Properties("GraphTest") {
def find[K,V](walkfn: Set[K] => Map[K,Set[K]], endsOnly: Boolean)(
s: Set[K], acc: Set[K] = Set[K](), visited: Set[K] = Set[K]()): Set[K] = {
if(s.isEmpty) {
acc
}
abstract class Marker(val g: AnyRef)
// Codegen 100 - 1000 of these, or use ASM to generate them on the fly
class Marker1(g: AnyRef) extends Marker(g)
class Marker2(g: AnyRef) extends Marker(g)
type Fn = (AnyRef) => Marker
class Markers(markers: List[(Fn, Class[_])], mmap: Map[Manifest[T], (Fn, Class[_])] = Map.empty) {
def get[T](implicit mf: Manifest[T]): (Fn, Class[_], Markers) = {
mmap.get(mf) { case (fn,cls) =>
@johnynek
johnynek / test.scala
Last active December 12, 2015 12:39 — forked from sritchie/test.scala
import annotation.implicitNotFound
@implicitNotFound(msg = "This message can never appear!")
trait ~>[A, B] { self =>
def apply(a: A): B
def invert(b: B): A = inverse.apply(b)
def inverse: B ~> A = new ~>[B,A] {
def apply(b: B) = self.invert(b)
override def invert(a: A) = self(a)
}
package com.twitter.bijection
/** Deals with the type-system issue around resolving implicit bijections.
* Bijection[A,B] or Bijection[B,A], which should
* be equivalent. Only use this type as an implicit parameter.
*/
import scala.annotation.implicitNotFound
@implicitNotFound(msg = "Cannot find ImplicitBijection type class from ${A} to ${B}")
sealed trait ImplicitBijection[A, B] extends (A => B) with java.io.Serializable {
def bijection: Bijection[A, B]
#!/bin/sh
exec scala -savecompiled "$0" "$@"
!#
/**
* You should be able to run this file if you have scala installed:
* either make it executable, or run it with: "scala MapReduceToy.scala < someInputFile.txt"
*/
// Toy Map Reduce framework:
class MapReduce[T,K,V,R](flatMapFn: (T) => Iterable[(K,V)], reduceFn: ((K,Iterable[V])) => R) {
@johnynek
johnynek / click_rate.scala
Created February 19, 2013 23:23
Example of computing click-rate in map/reduce style.
#!/bin/sh
exec scala -savecompiled "$0" "$@"
!#
// Toy Map Reduce framework:
class MapReduce[T,K,V,R](flatMapFn: (T) => Iterable[(K,V)], reduceFn: ((K,Iterable[V])) => R) {
def apply(input: Iterable[T]): Map[K,R] = {
// Apply the flatMap function:
val mapped: Iterable[(K,V)] = input.flatMap(flatMapFn)
@johnynek
johnynek / build_index.scala
Created February 19, 2013 23:41
Example of building a very simple search index with map-reduce.
#!/bin/sh
exec scala -savecompiled "$0" "$@"
!#
// Toy Map Reduce framework:
class MapReduce[T,K,V,R](flatMapFn: (T) => Iterable[(K,V)], reduceFn: ((K,Iterable[V])) => R) {
def apply(input: Iterable[T]): Map[K,R] = {
// Apply the flatMap function:
val mapped: Iterable[(K,V)] = input.flatMap(flatMapFn)
@johnynek
johnynek / topk_click_rate.scala
Created February 19, 2013 23:45
2 Map/Reduce steps to compute the top values for click-rate.
#!/bin/sh
exec scala -savecompiled "$0" "$@"
!#
// Toy Map Reduce framework:
class MapReduce[T,K,V,R](flatMapFn: (T) => Iterable[(K,V)], reduceFn: ((K,Iterable[V])) => R) {
def apply(input: Iterable[T]): Map[K,R] = {
// Apply the flatMap function:
val mapped: Iterable[(K,V)] = input.flatMap(flatMapFn)