Skip to content

Commit

Permalink
Fix impure repositories in cats-effect examples
Browse files Browse the repository at this point in the history
  • Loading branch information
neko-kai committed Jun 19, 2024
1 parent 1b85cee commit cf1548a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
package leaderboard.repo

import cats.{Applicative, Monad}
import cats.implicits.*
import cats.Monad
import cats.effect.{Concurrent, Ref}
import cats.syntax.all.*
import distage.Lifecycle
import doobie.postgres.implicits.*
import doobie.syntax.string.*
import leaderboard.model.{Score, UserId}
import leaderboard.sql.SQL
import logstage.LogIO

import scala.collection.concurrent.TrieMap

trait Ladder[F[_]] {
def submitScore(userId: UserId, score: Score): F[Unit]
def getScores: F[List[(UserId, Score)]]
}

object Ladder {
final class Dummy[F[_]: Applicative]
final class Dummy[F[_]: Concurrent]
extends Lifecycle.LiftF[F, Ladder[F]](for {
state <- Applicative[F].pure(TrieMap.empty[UserId, Score])
state <- Ref.of(Map.empty[UserId, Score])
} yield {
new Ladder[F] {
override def submitScore(userId: UserId, score: Score): F[Unit] =
Applicative[F].pure(state.update(userId, score))
state.update(_ + (userId -> score))

override def getScores: F[List[(UserId, Score)]] =
Applicative[F].pure(state.toList.sortBy(_._2)(Ordering[Score].reverse))
state.get.map(_.toList.sortBy(_._2)(Ordering[Score].reverse))
}
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
package leaderboard.repo

import cats.Monad
import cats.effect.{Concurrent, Ref}
import cats.syntax.all.*
import distage.Lifecycle
import cats.{Applicative, Monad}
import cats.implicits.*
import doobie.postgres.implicits.*
import doobie.syntax.string.*
import leaderboard.model.{UserId, UserProfile}
import leaderboard.sql.SQL
import logstage.LogIO

import scala.collection.concurrent.TrieMap

trait Profiles[F[_]] {
def setProfile(userId: UserId, profile: UserProfile): F[Unit]
def getProfile(userId: UserId): F[Option[UserProfile]]
}

object Profiles {
final class Dummy[F[_]: Applicative]
final class Dummy[F[_]: Concurrent]
extends Lifecycle.LiftF[F, Profiles[F]](for {
state <- Applicative[F].pure(TrieMap.empty[UserId, UserProfile])
state <- Ref.of(Map.empty[UserId, UserProfile])
} yield {
new Profiles[F] {
override def setProfile(userId: UserId, profile: UserProfile): F[Unit] =
Applicative[F].pure(state.update(userId, profile))
state.update(_ + (userId -> profile))

override def getProfile(userId: UserId): F[Option[UserProfile]] =
Applicative[F].pure(state.get(userId))
state.get.map(_.get(userId))
}
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package leaderboard.repo

import cats.effect.IO
import cats.implicits.*
import cats.effect.{IO, Ref}
import distage.Lifecycle
import doobie.postgres.implicits.*
import doobie.syntax.string.*
import leaderboard.model.{Score, UserId}
import leaderboard.sql.SQL
import logstage.LogIO

import scala.collection.concurrent.TrieMap

trait Ladder {
def submitScore(userId: UserId, score: Score): IO[Unit]
def getScores: IO[List[(UserId, Score)]]
Expand All @@ -19,14 +16,14 @@ trait Ladder {
object Ladder {
final class Dummy
extends Lifecycle.LiftF[IO, Ladder](for {
state <- IO.pure(TrieMap.empty[UserId, Score])
state <- Ref[IO].of(Map.empty[UserId, Score])
} yield {
new Ladder {
override def submitScore(userId: UserId, score: Score): IO[Unit] =
IO.pure(state.update(userId, score))
state.update(_ + (userId -> score))

override def getScores: IO[List[(UserId, Score)]] =
IO.pure(state.toList.sortBy(_._2)(Ordering[Score].reverse))
state.get.map(_.toList.sortBy(_._2)(Ordering[Score].reverse))
}
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package leaderboard.repo

import cats.effect.IO
import cats.effect.{IO, Ref}
import distage.Lifecycle
import doobie.postgres.implicits.*
import doobie.syntax.string.*
import leaderboard.model.{UserId, UserProfile}
import leaderboard.sql.SQL
import logstage.LogIO

import scala.collection.concurrent.TrieMap

trait Profiles {
def setProfile(userId: UserId, profile: UserProfile): IO[Unit]
def getProfile(userId: UserId): IO[Option[UserProfile]]
Expand All @@ -18,14 +16,14 @@ trait Profiles {
object Profiles {
final class Dummy
extends Lifecycle.LiftF[IO, Profiles](for {
state <- IO.pure(TrieMap.empty[UserId, UserProfile])
state <- Ref[IO].of(Map.empty[UserId, UserProfile])
} yield {
new Profiles {
override def setProfile(userId: UserId, profile: UserProfile): IO[Unit] =
IO.pure(state.update(userId, profile))
state.update(_ + (userId -> profile))

override def getProfile(userId: UserId): IO[Option[UserProfile]] =
IO.pure(state.get(userId))
state.get.map(_.get(userId))
}
})

Expand Down

0 comments on commit cf1548a

Please sign in to comment.