pre-commit
This commit is contained in:
parent
5727a52415
commit
ed12f5c442
6 changed files with 49 additions and 34 deletions
|
|
@ -12,7 +12,10 @@ object Condition:
|
|||
def apply[F[_]: Applicative, Input](
|
||||
comparison: Input,
|
||||
operator: Monoid[Input]
|
||||
)(using CanEqual[Input, Input]): Condition[F, Input] =
|
||||
)(
|
||||
using
|
||||
CanEqual[Input, Input]
|
||||
): Condition[F, Input] =
|
||||
new DefaultCondition[F, Input](comparison, operator)
|
||||
|
||||
def forInt[F[_]: Applicative](
|
||||
|
|
@ -31,20 +34,27 @@ object Condition:
|
|||
new Monoid[Int] {
|
||||
override def empty: Int = 0
|
||||
|
||||
override def combine(x: Int, y: Int): Int = op(x, y)
|
||||
override def combine(
|
||||
x: Int,
|
||||
y: Int
|
||||
): Int = op(x, y)
|
||||
}
|
||||
|
||||
private def makeLongMonoid(op: (Long, Long) => Long): Monoid[Long] =
|
||||
new Monoid[Long] {
|
||||
override def empty: Long = 0
|
||||
|
||||
override def combine(x: Long, y: Long): Long = op(x, y)
|
||||
override def combine(
|
||||
x: Long,
|
||||
y: Long
|
||||
): Long = op(x, y)
|
||||
}
|
||||
|
||||
def noop[F[_]: Applicative, Input]: Condition[F, Input] = new Noop[F, Input]
|
||||
|
||||
final class Noop[F[_]: Applicative, Input] extends Condition[F, Input]:
|
||||
override def matches(input: Input): F[Boolean] =
|
||||
|
||||
override def matches(input: Input): F[Boolean] =
|
||||
Applicative[F].pure(false)
|
||||
|
||||
end Condition
|
||||
|
|
|
|||
|
|
@ -6,9 +6,12 @@ import cats.Monoid
|
|||
final class DefaultCondition[F[_]: Applicative, Input](
|
||||
val comparison: Input,
|
||||
val operator: Monoid[Input]
|
||||
)(using CanEqual[Input, Input]) extends Condition[F, Input]:
|
||||
)(
|
||||
using
|
||||
CanEqual[Input, Input]
|
||||
) extends Condition[F, Input]:
|
||||
|
||||
override def matches(input: Input): F[Boolean] =
|
||||
override def matches(input: Input): F[Boolean] =
|
||||
Applicative[F].pure(
|
||||
operator.combine(input, comparison) == operator.empty
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,24 +1,21 @@
|
|||
package gs.fizzbuzz
|
||||
|
||||
import cats.syntax.all.*
|
||||
import cats.FlatMap
|
||||
import cats.syntax.all.*
|
||||
|
||||
class DefaultFizzBuzz[F[_]: FlatMap, Input](
|
||||
val fizzCondition: Condition[F, Input],
|
||||
val buzzCondition: Condition[F, Input]
|
||||
) extends FizzBuzz[F, Input]:
|
||||
def evaluate(input: Input): F[Result] =
|
||||
|
||||
def evaluate(input: Input): F[Result] =
|
||||
for
|
||||
fizz <- fizzCondition.matches(input)
|
||||
buzz <- buzzCondition.matches(input)
|
||||
yield
|
||||
if fizz && buzz then
|
||||
Result.FizzBuzz
|
||||
else if fizz then
|
||||
Result.Fizz
|
||||
else if buzz then
|
||||
Result.Buzz
|
||||
else
|
||||
Result.Unmatched
|
||||
yield
|
||||
if fizz && buzz then Result.FizzBuzz
|
||||
else if fizz then Result.Fizz
|
||||
else if buzz then Result.Buzz
|
||||
else Result.Unmatched
|
||||
|
||||
end DefaultFizzBuzz
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
package gs.fizzbuzz
|
||||
|
||||
import fs2.Pipe
|
||||
import cats.Applicative
|
||||
import cats.FlatMap
|
||||
import fs2.Pipe
|
||||
|
||||
trait FizzBuzz[F[_], Input]:
|
||||
def evaluate(input: Input): F[Result]
|
||||
def evaluate(input: Input): F[Result]
|
||||
def pipe(): Pipe[F, Input, Result] = in => in.evalMap(evaluate)
|
||||
end FizzBuzz
|
||||
|
||||
|
|
@ -14,7 +14,8 @@ object FizzBuzz:
|
|||
def apply[F[_]: FlatMap, Input](
|
||||
fizzCondition: Condition[F, Input],
|
||||
buzzCondition: Condition[F, Input]
|
||||
): FizzBuzz[F, Input] = new DefaultFizzBuzz[F, Input](fizzCondition, buzzCondition)
|
||||
): FizzBuzz[F, Input] =
|
||||
new DefaultFizzBuzz[F, Input](fizzCondition, buzzCondition)
|
||||
|
||||
def forInt[F[_]: FlatMap: Applicative](
|
||||
fizz: Int,
|
||||
|
|
@ -37,7 +38,8 @@ object FizzBuzz:
|
|||
def noop[F[_]: Applicative, Input]: FizzBuzz[F, Input] = new Noop[F, Input]
|
||||
|
||||
final class Noop[F[_]: Applicative, Input] extends FizzBuzz[F, Input]:
|
||||
override def evaluate(input: Input): F[Result] =
|
||||
|
||||
override def evaluate(input: Input): F[Result] =
|
||||
Applicative[F].pure(Result.Unmatched)
|
||||
|
||||
end FizzBuzz
|
||||
|
|
|
|||
|
|
@ -1,25 +1,28 @@
|
|||
package gs.fizzbuzz
|
||||
|
||||
import cats.effect.ExitCode
|
||||
import cats.effect.IO
|
||||
import cats.effect.IOApp
|
||||
import cats.effect.ExitCode
|
||||
|
||||
object Main extends IOApp:
|
||||
override def run(args: List[String]): IO[ExitCode] =
|
||||
|
||||
override def run(args: List[String]): IO[ExitCode] =
|
||||
val fb = FizzBuzz.forLong[IO](
|
||||
fizz = 3,
|
||||
buzz = 5,
|
||||
f = (x, y) => x % y
|
||||
f = (
|
||||
x,
|
||||
y
|
||||
) => x % y
|
||||
)
|
||||
|
||||
for
|
||||
for
|
||||
ex1 <- fb.evaluate(15)
|
||||
ex2 <- fb.evaluate(9)
|
||||
ex3 <- fb.evaluate(20)
|
||||
ex4 <- fb.evaluate(13)
|
||||
_ <- IO.println(ex1)
|
||||
_ <- IO.println(ex2)
|
||||
_ <- IO.println(ex3)
|
||||
_ <- IO.println(ex4)
|
||||
yield
|
||||
ExitCode.Success
|
||||
_ <- IO.println(ex1)
|
||||
_ <- IO.println(ex2)
|
||||
_ <- IO.println(ex3)
|
||||
_ <- IO.println(ex4)
|
||||
yield ExitCode.Success
|
||||
|
|
|
|||
|
|
@ -4,9 +4,9 @@ sealed trait Result
|
|||
|
||||
object Result:
|
||||
|
||||
case object Fizz extends Result
|
||||
case object Buzz extends Result
|
||||
case object FizzBuzz extends Result
|
||||
case object Fizz extends Result
|
||||
case object Buzz extends Result
|
||||
case object FizzBuzz extends Result
|
||||
case object Unmatched extends Result
|
||||
|
||||
end Result
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue