47 lines
1.2 KiB
Scala
47 lines
1.2 KiB
Scala
package tf.bug.example
|
|
|
|
import java.io.File
|
|
|
|
import cats._
|
|
import cats.data._
|
|
import cats.implicits._
|
|
import cats.effect.IO
|
|
import tf.bug.cataquack.{Read, Storage}
|
|
import tf.bug.cataquack.implicits._
|
|
|
|
import scala.io.Source
|
|
|
|
object Example {
|
|
|
|
def main(args: Array[String]): Unit = {
|
|
val f: File = new File("counter.txt")
|
|
val s: Storage[IO, String] = (key: String) => IO {
|
|
Source.fromFile(f).getLines().find(_.startsWith(key ++ " ")) match {
|
|
case Some(line) => line.drop(key.length + 1)
|
|
case None => throw new IllegalArgumentException(s"No such key: $key")
|
|
}
|
|
}
|
|
val count = readCount(s)
|
|
val desc = readDescription(s)
|
|
val pr = for {
|
|
c <- count
|
|
d <- desc
|
|
} yield println(s"$d: $c")
|
|
pr.unsafeRunSync()
|
|
}
|
|
|
|
case class Counter(n: Long)
|
|
case class Description(n: String)
|
|
|
|
def readCount[F[_]: Functor, T](s: Storage[F, T])(implicit read: Read[T, Long]): F[Long] = {
|
|
s.get("count").map(read.apply)
|
|
}
|
|
|
|
def readDescription[F[_]: Functor, T](s: Storage[F, T])(implicit read: Read[T, String]): F[String] = {
|
|
s.get("desc").map(read.apply)
|
|
}
|
|
|
|
implicit def readStringLong: Read[String, Long] = (i: String) => i.toLong
|
|
|
|
}
|