Skip to content

Instantly share code, notes, and snippets.

softberries

Block or report user

Report or block softberries

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View play-fp2-2.scala
package services
import cats.MonadError
import models.Person
import repositories.PersonRepository
import cats.implicits._
class PersonService[F[_]](personRepository: PersonRepository[F])(
implicit monadError: MonadError[F, String]
) {
View play-fp2-1.scala
package repositories
import models.Person
trait PersonRepository[F[_]] {
def get(name: String): F[Option[Person]]
def create(name: String, age: Int): F[Person]
def list(): F[Seq[Person]]
}
View play-fp-11.scala
IO.pure(Ok(views.html.index(errorForm)))
View play-fp-10.scala
def getPersons = Action.asyncF { implicit request =>
repo.list().map { people =>
Ok(Json.toJson(people))
}
}
View play-fp-9.scala
object IOHttp {
implicit class ActionBuilderOps[+R[_], B](ab: ActionBuilder[R, B]) {
import cats.effect.implicits._
def asyncF[F[_] : Effect](cb: R[B] => F[Result]): Action[B] = ab.async { c =>
cb(c).toIO.unsafeToFuture()
}
}
View play-fp-8.scala
def getPersons = Action.async { implicit request =>
repo.list().map { people =>
Ok(Json.toJson(people))
}.unsafeToFuture()
}
View play-fp-7.scala
@Singleton
class PersonRepository @Inject()(dbConfigProvider: DatabaseConfigProvider)
(implicit ec: ExecutionContext) {
implicit val cs = IO.contextShift(ec)
.....
View play-fp-6.scala
def list(): IO[Seq[Person]] = IO.fromFuture {
IO {
db.run {
people.result
}
}
View play-fp-5.scala
val ioa2 = IO.fromFuture(IO {
Future { println("hey fut 2!") }
})
val program2 =
for {
_ <- ioa2
_ <- ioa2
} yield ()
program2.unsafeRunSync()
View play-fp-4.scala
val fut = Future { println("hey fut!") }
val ioa = IO.fromFuture(IO {
fut
})
val program =
for {
_ <- ioa
_ <- ioa
} yield ()
program.unsafeRunSync()
You can’t perform that action at this time.