Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. import cats.Applicative
  2. import cats.instances.list._
  3. import cats.instances.option._
  4. import cats.syntax.functor._
  5.  
  6. trait Instance[TC[_]] {
  7. type Type
  8. def value: Type
  9. def typeclass: TC[Type]
  10. }
  11. object Instance {
  12. implicit def apply[A, TC[_]](a: A)(implicit A: TC[A]): Instance[TC] = new Instance[TC] {
  13. type Type = A
  14. val value = a
  15. val typeclass = A
  16. }
  17. }
  18.  
  19. trait I18n[F[_]] {
  20.  
  21. }
  22.  
  23. trait Format[A] { def format[F[_]](a: A)(implicit F: Applicative[F], I: I18n[F]): F[String] }
  24. object Format {
  25. def universal[A]: Format[A] = new Format[A] {
  26. def format[F[_]](a: A)(implicit F: Applicative[F], I: I18n[F]) = F.pure(a.toString)
  27. }
  28.  
  29. implicit val showInt: Format[Int] = universal[Int]
  30. implicit val showBool: Format[Boolean] = universal[Boolean]
  31. implicit val showStr: Format[String] = universal[String]
  32. }
  33.  
  34. implicit final class I18nStringContext(val sc: StringContext) extends AnyVal {
  35. def intersperse[A](a : List[A], b : List[A]): List[A] = a match {
  36. case first :: rest => first :: intersperse(b, rest)
  37. case _ => b
  38. }
  39.  
  40. def i18n[F[_]](args: Instance[Format]*)(implicit I: I18n[F], F: Applicative[F]): F[String] =
  41. F.traverse(args.toList)(i => i.typeclass.format[F](i.value)(F, I))
  42. .map(vars => intersperse(sc.parts.toList, vars).mkString(""))
  43. }
  44.  
  45. implicit val i18nEval: I18n[Option] = new I18n[Option] { }
  46.  
  47. val x: Option[String] = i18n"${10 < 12} ${1 + 1}"
  48. println(x)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement