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](
|
def apply[F[_]: Applicative, Input](
|
||||||
comparison: Input,
|
comparison: Input,
|
||||||
operator: Monoid[Input]
|
operator: Monoid[Input]
|
||||||
)(using CanEqual[Input, Input]): Condition[F, Input] =
|
)(
|
||||||
|
using
|
||||||
|
CanEqual[Input, Input]
|
||||||
|
): Condition[F, Input] =
|
||||||
new DefaultCondition[F, Input](comparison, operator)
|
new DefaultCondition[F, Input](comparison, operator)
|
||||||
|
|
||||||
def forInt[F[_]: Applicative](
|
def forInt[F[_]: Applicative](
|
||||||
|
|
@ -31,19 +34,26 @@ object Condition:
|
||||||
new Monoid[Int] {
|
new Monoid[Int] {
|
||||||
override def empty: Int = 0
|
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] =
|
private def makeLongMonoid(op: (Long, Long) => Long): Monoid[Long] =
|
||||||
new Monoid[Long] {
|
new Monoid[Long] {
|
||||||
override def empty: Long = 0
|
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]
|
def noop[F[_]: Applicative, Input]: Condition[F, Input] = new Noop[F, Input]
|
||||||
|
|
||||||
final class Noop[F[_]: Applicative, Input] extends Condition[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)
|
Applicative[F].pure(false)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,10 @@ import cats.Monoid
|
||||||
final class DefaultCondition[F[_]: Applicative, Input](
|
final class DefaultCondition[F[_]: Applicative, Input](
|
||||||
val comparison: Input,
|
val comparison: Input,
|
||||||
val operator: Monoid[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(
|
Applicative[F].pure(
|
||||||
|
|
|
||||||
|
|
@ -1,24 +1,21 @@
|
||||||
package gs.fizzbuzz
|
package gs.fizzbuzz
|
||||||
|
|
||||||
import cats.syntax.all.*
|
|
||||||
import cats.FlatMap
|
import cats.FlatMap
|
||||||
|
import cats.syntax.all.*
|
||||||
|
|
||||||
class DefaultFizzBuzz[F[_]: FlatMap, Input](
|
class DefaultFizzBuzz[F[_]: FlatMap, Input](
|
||||||
val fizzCondition: Condition[F, Input],
|
val fizzCondition: Condition[F, Input],
|
||||||
val buzzCondition: Condition[F, Input]
|
val buzzCondition: Condition[F, Input]
|
||||||
) extends FizzBuzz[F, Input]:
|
) extends FizzBuzz[F, Input]:
|
||||||
|
|
||||||
def evaluate(input: Input): F[Result] =
|
def evaluate(input: Input): F[Result] =
|
||||||
for
|
for
|
||||||
fizz <- fizzCondition.matches(input)
|
fizz <- fizzCondition.matches(input)
|
||||||
buzz <- buzzCondition.matches(input)
|
buzz <- buzzCondition.matches(input)
|
||||||
yield
|
yield
|
||||||
if fizz && buzz then
|
if fizz && buzz then Result.FizzBuzz
|
||||||
Result.FizzBuzz
|
else if fizz then Result.Fizz
|
||||||
else if fizz then
|
else if buzz then Result.Buzz
|
||||||
Result.Fizz
|
else Result.Unmatched
|
||||||
else if buzz then
|
|
||||||
Result.Buzz
|
|
||||||
else
|
|
||||||
Result.Unmatched
|
|
||||||
|
|
||||||
end DefaultFizzBuzz
|
end DefaultFizzBuzz
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
package gs.fizzbuzz
|
package gs.fizzbuzz
|
||||||
|
|
||||||
import fs2.Pipe
|
|
||||||
import cats.Applicative
|
import cats.Applicative
|
||||||
import cats.FlatMap
|
import cats.FlatMap
|
||||||
|
import fs2.Pipe
|
||||||
|
|
||||||
trait FizzBuzz[F[_], Input]:
|
trait FizzBuzz[F[_], Input]:
|
||||||
def evaluate(input: Input): F[Result]
|
def evaluate(input: Input): F[Result]
|
||||||
|
|
@ -14,7 +14,8 @@ object FizzBuzz:
|
||||||
def apply[F[_]: FlatMap, Input](
|
def apply[F[_]: FlatMap, Input](
|
||||||
fizzCondition: Condition[F, Input],
|
fizzCondition: Condition[F, Input],
|
||||||
buzzCondition: 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](
|
def forInt[F[_]: FlatMap: Applicative](
|
||||||
fizz: Int,
|
fizz: Int,
|
||||||
|
|
@ -37,6 +38,7 @@ object FizzBuzz:
|
||||||
def noop[F[_]: Applicative, Input]: FizzBuzz[F, Input] = new Noop[F, Input]
|
def noop[F[_]: Applicative, Input]: FizzBuzz[F, Input] = new Noop[F, Input]
|
||||||
|
|
||||||
final class Noop[F[_]: Applicative, Input] extends FizzBuzz[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)
|
Applicative[F].pure(Result.Unmatched)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,19 @@
|
||||||
package gs.fizzbuzz
|
package gs.fizzbuzz
|
||||||
|
|
||||||
|
import cats.effect.ExitCode
|
||||||
import cats.effect.IO
|
import cats.effect.IO
|
||||||
import cats.effect.IOApp
|
import cats.effect.IOApp
|
||||||
import cats.effect.ExitCode
|
|
||||||
|
|
||||||
object Main extends IOApp:
|
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](
|
val fb = FizzBuzz.forLong[IO](
|
||||||
fizz = 3,
|
fizz = 3,
|
||||||
buzz = 5,
|
buzz = 5,
|
||||||
f = (x, y) => x % y
|
f = (
|
||||||
|
x,
|
||||||
|
y
|
||||||
|
) => x % y
|
||||||
)
|
)
|
||||||
|
|
||||||
for
|
for
|
||||||
|
|
@ -17,9 +21,8 @@ object Main extends IOApp:
|
||||||
ex2 <- fb.evaluate(9)
|
ex2 <- fb.evaluate(9)
|
||||||
ex3 <- fb.evaluate(20)
|
ex3 <- fb.evaluate(20)
|
||||||
ex4 <- fb.evaluate(13)
|
ex4 <- fb.evaluate(13)
|
||||||
_ <- IO.println(ex1)
|
_ <- IO.println(ex1)
|
||||||
_ <- IO.println(ex2)
|
_ <- IO.println(ex2)
|
||||||
_ <- IO.println(ex3)
|
_ <- IO.println(ex3)
|
||||||
_ <- IO.println(ex4)
|
_ <- IO.println(ex4)
|
||||||
yield
|
yield ExitCode.Success
|
||||||
ExitCode.Success
|
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,9 @@ sealed trait Result
|
||||||
|
|
||||||
object Result:
|
object Result:
|
||||||
|
|
||||||
case object Fizz extends Result
|
case object Fizz extends Result
|
||||||
case object Buzz extends Result
|
case object Buzz extends Result
|
||||||
case object FizzBuzz extends Result
|
case object FizzBuzz extends Result
|
||||||
case object Unmatched extends Result
|
case object Unmatched extends Result
|
||||||
|
|
||||||
end Result
|
end Result
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue