Skip to content

Instantly share code, notes, and snippets.

@yasuabe
Created December 25, 2018 10:35
Show Gist options
  • Save yasuabe/10f2f90a3b6135f921e2fe1cb63eb596 to your computer and use it in GitHub Desktop.
Save yasuabe/10f2f90a3b6135f921e2fe1cb63eb596 to your computer and use it in GitHub Desktop.
package representable01
import cats.instances.AllInstances
import cats.laws.discipline.RepresentableTests
import cats.{Eq, Functor, Representable}
import org.scalacheck.{Arbitrary, Gen}
import org.specs2.Specification
import org.typelevel.discipline.specs2.Discipline
import cats.instances.stream._
object RepresentableStream {
implicit val rep: Representable.Aux[Stream, Int] = new Representable[Stream] {
type Representation = Int
def F: Functor[Stream] = implicitly[Functor[Stream]]
def tabulate[A](f: Int => A): Stream[A] = Stream.from(0) map f
def index[A](f: Stream[A]): Int => A = f.apply
}
}
import RepresentableStream._
class StreamSpec extends Specification with Discipline with AllInstances {
val genNonNegative: Gen[Int] = Gen.chooseNum(0, 4)
val genChar: Gen[Char] = Gen.oneOf('a', 'b', 'c')
val genCharStream: Gen[Stream[Char]] = Gen.listOfN(5, genChar).map(_.toStream)
implicit val arbNonNegative: Arbitrary[Int] = Arbitrary(genNonNegative)
implicit val arbCharStream: Arbitrary[Stream[Char]] = Arbitrary(genCharStream)
implicit val eqCharStream: Eq[Stream[Char]] = (x, y) => x.take(5) == y.take(5)
private val check = checkAll("Representable of Stream", RepresentableTests[Stream, Int].representable[Char])
def is = s2"Representable of Stream satisfies representable laws $check"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment