This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) => |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |