Compare commits
2 commits
439c28c1c7
...
ed12f5c442
| Author | SHA1 | Date | |
|---|---|---|---|
| ed12f5c442 | |||
| 5727a52415 |
6 changed files with 126 additions and 65 deletions
|
|
@ -1,18 +1,60 @@
|
|||
package gs.fizzbuzz
|
||||
|
||||
import cats.Applicative
|
||||
import cats.Monoid
|
||||
|
||||
trait Condition[F[_], Input, Output]:
|
||||
trait Condition[F[_], Input]:
|
||||
def matches(input: Input): F[Boolean]
|
||||
def output: Output
|
||||
end Condition
|
||||
|
||||
object Condition:
|
||||
|
||||
final class Noop[F[_]: Applicative, Input] extends Condition[F, Input, Nothing]:
|
||||
def apply[F[_]: Applicative, Input](
|
||||
comparison: Input,
|
||||
operator: Monoid[Input]
|
||||
)(
|
||||
using
|
||||
CanEqual[Input, Input]
|
||||
): Condition[F, Input] =
|
||||
new DefaultCondition[F, Input](comparison, operator)
|
||||
|
||||
def forInt[F[_]: Applicative](
|
||||
comparison: Int,
|
||||
operator: (Int, Int) => Int
|
||||
): Condition[F, Int] =
|
||||
new DefaultCondition[F, Int](comparison, makeIntMonoid(operator))
|
||||
|
||||
def forLong[F[_]: Applicative](
|
||||
comparison: Long,
|
||||
operator: (Long, Long) => Long
|
||||
): Condition[F, Long] =
|
||||
new DefaultCondition[F, Long](comparison, makeLongMonoid(operator))
|
||||
|
||||
private def makeIntMonoid(op: (Int, Int) => Int): Monoid[Int] =
|
||||
new Monoid[Int] {
|
||||
override def empty: Int = 0
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
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] =
|
||||
Applicative[F].pure(false)
|
||||
override def output: Nothing =
|
||||
throw new IllegalStateException("No-op condition cannot produce output.")
|
||||
|
||||
end Condition
|
||||
|
|
|
|||
|
|
@ -3,11 +3,13 @@ package gs.fizzbuzz
|
|||
import cats.Applicative
|
||||
import cats.Monoid
|
||||
|
||||
final class DefaultCondition[F[_]: Applicative, Input, Out](
|
||||
final class DefaultCondition[F[_]: Applicative, Input](
|
||||
val comparison: Input,
|
||||
val output: Out,
|
||||
val operator: Monoid[Input]
|
||||
)(using CanEqual[Input, Input]) extends Condition[F, Input, Out]:
|
||||
)(
|
||||
using
|
||||
CanEqual[Input, Input]
|
||||
) extends Condition[F, Input]:
|
||||
|
||||
override def matches(input: Input): F[Boolean] =
|
||||
Applicative[F].pure(
|
||||
|
|
|
|||
|
|
@ -1,26 +1,21 @@
|
|||
package gs.fizzbuzz
|
||||
|
||||
import cats.syntax.all.*
|
||||
import fs2.Pipe
|
||||
import cats.FlatMap
|
||||
import cats.syntax.all.*
|
||||
|
||||
class DefaultFizzBuzz[F[_]: FlatMap, Input, Out](
|
||||
val fizzCondition: Condition[F, Input, Out],
|
||||
val buzzCondition: Condition[F, Input, Out]
|
||||
) extends FizzBuzz[F, Input, Out]:
|
||||
def evaluate(input: Input): F[Result[Out]] =
|
||||
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] =
|
||||
for
|
||||
fizz <- fizzCondition.matches(input)
|
||||
buzz <- buzzCondition.matches(input)
|
||||
yield
|
||||
if fizz && buzz then
|
||||
Result.FizzBuzz(fizzCondition.output, buzzCondition.output)
|
||||
else if fizz then
|
||||
Result.Fizz(fizzCondition.output)
|
||||
else if buzz then
|
||||
Result.Buzz(buzzCondition.output)
|
||||
else
|
||||
Result.Unmatched
|
||||
if fizz && buzz then Result.FizzBuzz
|
||||
else if fizz then Result.Fizz
|
||||
else if buzz then Result.Buzz
|
||||
else Result.Unmatched
|
||||
|
||||
def pipe(): Pipe[F, Input, Result[Out]] = in => in.evalMap(evaluate)
|
||||
end DefaultFizzBuzz
|
||||
|
|
|
|||
|
|
@ -1,8 +1,45 @@
|
|||
package gs.fizzbuzz
|
||||
|
||||
import cats.Applicative
|
||||
import cats.FlatMap
|
||||
import fs2.Pipe
|
||||
|
||||
trait FizzBuzz[F[_], Input, Out]:
|
||||
def evaluate(input: Input): F[Result[Out]]
|
||||
def pipe(): Pipe[F, Input, Result[Out]]
|
||||
trait FizzBuzz[F[_], Input]:
|
||||
def evaluate(input: Input): F[Result]
|
||||
def pipe(): Pipe[F, Input, Result] = in => in.evalMap(evaluate)
|
||||
end FizzBuzz
|
||||
|
||||
object FizzBuzz:
|
||||
|
||||
def apply[F[_]: FlatMap, Input](
|
||||
fizzCondition: Condition[F, Input],
|
||||
buzzCondition: Condition[F, Input]
|
||||
): FizzBuzz[F, Input] =
|
||||
new DefaultFizzBuzz[F, Input](fizzCondition, buzzCondition)
|
||||
|
||||
def forInt[F[_]: FlatMap: Applicative](
|
||||
fizz: Int,
|
||||
buzz: Int,
|
||||
f: (Int, Int) => Int
|
||||
): FizzBuzz[F, Int] = new DefaultFizzBuzz[F, Int](
|
||||
fizzCondition = Condition.forInt[F](fizz, f),
|
||||
buzzCondition = Condition.forInt[F](buzz, f)
|
||||
)
|
||||
|
||||
def forLong[F[_]: FlatMap: Applicative](
|
||||
fizz: Long,
|
||||
buzz: Long,
|
||||
f: (Long, Long) => Long
|
||||
): FizzBuzz[F, Long] = new DefaultFizzBuzz[F, Long](
|
||||
fizzCondition = Condition.forLong[F](fizz, f),
|
||||
buzzCondition = Condition.forLong[F](buzz, f)
|
||||
)
|
||||
|
||||
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] =
|
||||
Applicative[F].pure(Result.Unmatched)
|
||||
|
||||
end FizzBuzz
|
||||
|
|
|
|||
|
|
@ -1,32 +1,19 @@
|
|||
package gs.fizzbuzz
|
||||
|
||||
import cats.Monoid
|
||||
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] =
|
||||
val op: Monoid[Long] = new Monoid[Long] {
|
||||
override def empty: Long = 0L
|
||||
override def combine(x: Long, y: Long): Long = x % y
|
||||
}
|
||||
|
||||
val fizzCond: Condition[IO, Long, String] = new DefaultCondition(
|
||||
comparison = 3L,
|
||||
output = "Fizz",
|
||||
operator = op
|
||||
)
|
||||
|
||||
val buzzCond: Condition[IO, Long, String] = new DefaultCondition(
|
||||
comparison = 5L,
|
||||
output = "Buzz",
|
||||
operator = op
|
||||
)
|
||||
|
||||
val fb: FizzBuzz[IO, Long, String] = new DefaultFizzBuzz[IO, Long, String](
|
||||
fizzCondition = fizzCond,
|
||||
buzzCondition = buzzCond
|
||||
val fb = FizzBuzz.forLong[IO](
|
||||
fizz = 3,
|
||||
buzz = 5,
|
||||
f = (
|
||||
x,
|
||||
y
|
||||
) => x % y
|
||||
)
|
||||
|
||||
for
|
||||
|
|
@ -34,9 +21,8 @@ object Main extends IOApp:
|
|||
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
|
||||
|
|
|
|||
|
|
@ -1,13 +1,12 @@
|
|||
package gs.fizzbuzz
|
||||
|
||||
sealed trait Result[+Out]
|
||||
sealed trait Result
|
||||
|
||||
object Result:
|
||||
|
||||
case class Fizz[Out](fizz: Out) extends Result[Out]
|
||||
case class Buzz[Out](buzz: Out) extends Result[Out]
|
||||
case class FizzBuzz[Out](fizz: Out, buzz: Out) extends Result[Out]
|
||||
case object Unmatched extends Result[Nothing]:
|
||||
override def toString(): String = "unmatched"
|
||||
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