Skip to content

Instantly share code, notes, and snippets.

View johnynek's full-sized avatar

P. Oscar Boykin johnynek

View GitHub Profile
@johnynek
johnynek / sane_for.scala
Created September 16, 2016 01:19
Scala's for syntax could have had a sane rule behind it. Why not something like this?
// 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]
@johnynek
johnynek / unsound.scala
Last active July 26, 2016 20:46
This is fine.
//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]
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] =
/**
* 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]
}
@johnynek
johnynek / scorching.scala
Created May 27, 2016 17:03
An AST for a data-flow API where serialization/shuffling is explicit
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] =
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)
}
}
}
@johnynek
johnynek / ForJeff.scala
Created December 28, 2015 21:03
Example of requiring two types to match but not caring later what the type is.
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)
}
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")
@johnynek
johnynek / SafeEq.scala
Last active January 22, 2016 21:46
A zero cost, type-safe equals function in scala
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
@johnynek
johnynek / 3 element semilattices
Created November 9, 2015 07:58
Brute force enumeration of all semilattices (and commutative semigroups) of finite size
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