Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cats.Applicative
- import cats.instances.list._
- import cats.instances.option._
- import cats.syntax.functor._
- trait Instance[TC[_]] {
- type Type
- def value: Type
- def typeclass: TC[Type]
- }
- object Instance {
- implicit def apply[A, TC[_]](a: A)(implicit A: TC[A]): Instance[TC] = new Instance[TC] {
- type Type = A
- val value = a
- val typeclass = A
- }
- }
- trait I18n[F[_]] {
- }
- trait Format[A] { def format[F[_]](a: A)(implicit F: Applicative[F], I: I18n[F]): F[String] }
- object Format {
- def universal[A]: Format[A] = new Format[A] {
- def format[F[_]](a: A)(implicit F: Applicative[F], I: I18n[F]) = F.pure(a.toString)
- }
- implicit val showInt: Format[Int] = universal[Int]
- implicit val showBool: Format[Boolean] = universal[Boolean]
- implicit val showStr: Format[String] = universal[String]
- }
- implicit final class I18nStringContext(val sc: StringContext) extends AnyVal {
- def intersperse[A](a : List[A], b : List[A]): List[A] = a match {
- case first :: rest => first :: intersperse(b, rest)
- case _ => b
- }
- def i18n[F[_]](args: Instance[Format]*)(implicit I: I18n[F], F: Applicative[F]): F[String] =
- F.traverse(args.toList)(i => i.typeclass.format[F](i.value)(F, I))
- .map(vars => intersperse(sc.parts.toList, vars).mkString(""))
- }
- implicit val i18nEval: I18n[Option] = new I18n[Option] { }
- val x: Option[String] = i18n"${10 < 12} ${1 + 1}"
- println(x)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement