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
| // WHY NOT SCALA!?! | |
| // Why not require this for for { } syntax?!? FOR THE LOVE OF GOD!? | |
| trait FlatMappable[F[+_], +A] { | |
| def flatMap[B](fn: A => F[B]): F[B] | |
| def map[B](fn: A => B): F[B] | |
| } | |
| sealed trait List[+T] extends FlatMappable[List, T] { | |
| def ++[U >: T](that: List[U]): List[U] |
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
| //st-oscar1:unsound oscar$ cat unsound.scala | |
| // inspired by http://typelevel.org/blog/2014/07/06/singleton_instance_trick_unsafe.html | |
| object Unsound { | |
| def changeType[A, B]: A => B = { | |
| case class Id[C](c: C) | |
| def nullId[A]: Id[A] = null | |
| val b: Id[B] = nullId[B] | |
| val a: Id[A] = nullId[A] | |
| a match { | |
| case _: b.type => implicitly[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
| package com.stripe.scorching | |
| import com.twitter.scalding.serialization.{OrderedSerialization, Serialization} | |
| sealed trait Collection[+T] { | |
| import Collection._ | |
| def concatMap[U](f: T => TraversableOnce[U]): Collection[U] = ConcatMap(this, f) | |
| def group[K, V](implicit ord: OrderedSerialization[K], | |
| vser: Serialization[V], | |
| ev: T <:< (K, V)): Grouping[K, V] = |
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
| /** | |
| * In a static language like scala, how could we repeatedly flatten a datastructure without reflection? | |
| * This is an interesting example of using implicit parameters to do the work for you. | |
| */ | |
| object DeepFlatten { | |
| // what should this really be called? ;) | |
| trait Flattenable[F[_]] { | |
| def flatten[A](f: F[F[A]]): F[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.stripe.scorching | |
| import com.twitter.scalding.serialization.{OrderedSerialization, Serialization} | |
| sealed trait Collection[+T] { | |
| import Collection._ | |
| def concatMap[U](f: T => TraversableOnce[U]): Collection[U] = ConcatMap(this, f) | |
| def group[K, V](implicit ord: OrderedSerialization[K], | |
| vser: Serialization[V], | |
| ev: T <:< (K, V)): Grouping[K, V] = |
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
| sealed trait PartialError[E, T] { | |
| def combine(e1: E, e2: E): E | |
| def flatMap[U](f: T => PartialError[E, U]) = this match { | |
| case Success(t) => f(t) | |
| case Partial(e1, t) => f(t) match { | |
| case Success(u) => Partial(e, u) | |
| case Partial(e2, u) => Partial(combine(e1, e2), u) | |
| } | |
| } | |
| } |
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
| object Foo { | |
| type Sink[T] = T => Unit | |
| type Flush[T] = (Iterator[T], Sink[T]) | |
| def flush[T](f: Flush[T]) = | |
| f._1.foreach(f._2) | |
| def flushAll(fs: Iterable[Flush[_]]) = | |
| fs.foreach(flush) | |
| } |
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.scalding.serialization.macros.impl | |
| import scala.language.experimental.macros | |
| import scala.reflect.macros.Context | |
| object CollectionMacros { | |
| private[this] def function1Apply[T, U](c: Context)(fn: c.Expr[T => U]) = { | |
| import c.universe._ | |
| val arg = newTermName("arg") |
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 scala.language.experimental.macros | |
| import scala.reflect.macros.Context | |
| /** | |
| * you can try this in the REPL even | |
| */ | |
| object SafeEq { | |
| /** | |
| * eqv(1, "foo") won't compile |
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
| There are 9 semilattices on [0,1,2]: | |
| 0 1 2 | |
| - - - | |
| 0| 0 0 0 | |
| 1| 0 1 0 | |
| 2| 0 0 2 | |
| ----------------------- | |
| 0 1 2 | |
| - - - | |
| 0| 0 0 0 |