0

I have defined repository:

trait Repo[F[_]] {
  def findAll: Stream[F, Stop]
}

and it's implementation:

class DoobieRepo[F[_]: Sync](xa: Transactor[F]) extends Repo[F] {

  override def findAll: Stream[F, Stop] =
    readAllStopsQ.stream.transact(xa).map{
      case (id, names, direction, lat, lon, typ) => Stop(id, names.split('|').toList, direction, lat, lon, typ)
    }

  def readAllStopsQ =
    sql"select * from stop".query[(String, String, String, Float, Float, String)]
}

In the application I'm doing following:

val transactor = Transactor.fromDriverManager[Task](
  "org.postgresql.Driver",
  "jdbc:postgresql:test",
  "me",
  ""
)

val repo = new DoobieRepo[Task](transactor)
repo.findAll // Stream[Task, Stop]

The question is how can I for example print this to console? (In general do some action with extracted data)

I tried:

repo.findAll.map { x =>
  println(x)
}

But unsuccessful

oybek
  • 630
  • 4
  • 14
  • 1
    The best would be something like `repo.findAll.evalMap(x => F.delay(println(x)))`. But this will return another **Stream**, to force it evaluation you may `s.compile.drain.unsafeRunSync()`. – Luis Miguel Mejía Suárez Aug 22 '19 at 14:20
  • @LuisMiguelMejíaSuárez but there is no `delay` method in `F` when I abstract over `Task`. `F` may be `IO` or whatever – oybek Aug 22 '19 at 17:38
  • 2
    @oybek the main point being that just mapping the stream doesn't *do* anything. You have to consume the stream somehow, e.g. via `.compile.drain.unsafeRunSync()` – Dylan Aug 22 '19 at 18:37
  • @oybek given you required your `F` to have an associated **Sync[F]** in scope. You can call `delay`, in your case, you can do `Sync[F].delay(...)`. – Luis Miguel Mejía Suárez Aug 22 '19 at 19:04
  • @LuisMiguelMejíaSuárez there is one more question, when I call `repo.findAll` I get `Stream[F, Stop]`, how can I convert it to `F[List[Stop]]`? I want to get all records from table one time and memoize them – oybek Aug 23 '19 at 06:05
  • @oybek just `stream.compile.toList` - However, it seems you are not very familiar with **FS2**, neither with **cats-effect**. I would suggest you to read the documentation of both first, they are pretty complex libraries. Also, **scaladoc** is your friend, the last question could have been easily solved with a little search on the `fs2-core` scaladoc. – Luis Miguel Mejía Suárez Aug 23 '19 at 12:01

0 Answers0