Skip to content

Instantly share code, notes, and snippets.

@yilinwei
Last active May 6, 2017 16:28
Show Gist options
  • Save yilinwei/ea93cc9d78dc7b372f6bfa2bf0d107f5 to your computer and use it in GitHub Desktop.
Save yilinwei/ea93cc9d78dc7b372f6bfa2bf0d107f5 to your computer and use it in GitHub Desktop.
spec
package fs2
trait Chunk2[@specialized(Boolean, Int, Long, Float) +A] {
def map[B](f: A => B): Chunk2[B]
}
object Chunk2 {
object Booleans {
val empty: Chunk2[Boolean] = new Chunk2[Boolean] {
override def map[B](f: Boolean => B): Chunk2[B] = ???
}
def unapply[A](chunk: Chunk2[A]): Option[Chunk2[Boolean]] = {
if(chunk.getClass.isAssignableFrom(empty.getClass)) {
Some(chunk.asInstanceOf[Chunk2[Boolean]])
} else None
}
}
def test[A](chunk: Chunk2[A]): Chunk2[A] = {
chunk match {
case Chunk2.Booleans(bChunk) => bChunk.map(_ && true).asInstanceOf[Chunk2[A]]
case _ => chunk
}
}
object Ints {
val empty: Chunk2[Int] = new Chunk2[Int] {
override def map[B](f: Int => B): Chunk2[B] = ???
}
def unapply[A](chunk: Chunk2[A]): Option[Chunk2[Int]] = {
if(chunk.getClass.isAssignableFrom(empty.getClass)) {
Some(chunk.asInstanceOf[Chunk2[Int]])
} else None
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment