Skip to content

Instantly share code, notes, and snippets.

@johnynek
Created September 16, 2016 01:19
Show Gist options
  • Select an option

  • Save johnynek/18f9f2a9ae7ebc8acd3fb82f0226ad9a to your computer and use it in GitHub Desktop.

Select an option

Save johnynek/18f9f2a9ae7ebc8acd3fb82f0226ad9a to your computer and use it in GitHub Desktop.
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]
}
case object Empty extends List[Nothing] {
def flatMap[B](fn: Nothing => List[B]): List[B] = Empty
def map[B](fn: Nothing => B): List[B] = Empty
def ++[U >: Nothing](that: List[U]): List[U] = that
}
case class Cons[T](head: T, tail: List[T]) extends List[T] {
def flatMap[B](fn: T => List[B]): List[B] =
fn(head) ++ tail.flatMap(fn)
def map[B](fn: T => B): List[B] = Cons(fn(head), tail.map(fn))
def ++[U >: T](that: List[U]): List[U] =
Cons(head, tail ++ that)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment