diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/src/main/scala/decode.scala | 65 | ||||
-rw-r--r-- | examples/src/main/scala/default.scala | 24 | ||||
-rw-r--r-- | examples/src/main/scala/eq.scala | 25 | ||||
-rw-r--r-- | examples/src/main/scala/show.scala | 26 | ||||
-rw-r--r-- | examples/src/main/scala/typeclasses.scala | 127 |
5 files changed, 140 insertions, 127 deletions
diff --git a/examples/src/main/scala/decode.scala b/examples/src/main/scala/decode.scala new file mode 100644 index 0000000..1cc8a12 --- /dev/null +++ b/examples/src/main/scala/decode.scala @@ -0,0 +1,65 @@ +package magnolia.examples + +import scala.language.existentials +import scala.language.higherKinds + +import magnolia._ +import scala.language.experimental.macros + +trait Decoder[T] { def decode(str: String): T } + +object Decoder { + + implicit val string: Decoder[String] = new Decoder[String] { def decode(str: String): String = str } + implicit val int: Decoder[Int] = new Decoder[Int] { def decode(str: String): Int = str.toInt } + implicit def generic[T]: Decoder[T] = macro Magnolia.generic[T] + + type Typeclass[T] = Decoder[T] + + def join[T](ctx: JoinContext[Decoder, T]): Decoder[T] = new Decoder[T] { + def decode(value: String) = { + val (name, values) = parse(value) + ctx.construct { param => param.typeclass.decode(values(param.label)) } + } + } + + def dispatch[T](ctx: DispatchContext[Decoder, T]): Decoder[T] = new Decoder[T] { + def decode(param: String) = { + val (name, values) = parse(param) + val subtype = ctx.subtypes.find(_.label == name).get + subtype.typeclass.decode(param) + } + } + + private def parse(value: String): (String, Map[String, String]) = { + val end = value.indexOf('(') + val name = value.substring(0, end) + + def parts(value: String, idx: Int = 0, depth: Int = 0, collected: List[String] = List("")): List[String] = { + def plus(char: Char): List[String] = collected.head+char :: collected.tail + + if(idx == value.length) collected + else value(idx) match { + case '(' => + parts(value, idx + 1, depth + 1, plus('(')) + case ')' => + if(depth == 1) plus(')') + else parts(value, idx + 1, depth - 1, plus(')')) + case ',' => + if(depth == 0) parts(value, idx + 1, depth, "" :: collected) + else parts(value, idx + 1, depth, plus(',')) + case char => + parts(value, idx + 1, depth, plus(char)) + } + } + + def keyValue(str: String): (String, String) = { + val List(label, value) = str.split("=", 2).to[List] + (label, value) + } + + (name, parts(value.substring(end + 1, value.length - 1)).map(keyValue).toMap) + } + +} + diff --git a/examples/src/main/scala/default.scala b/examples/src/main/scala/default.scala new file mode 100644 index 0000000..1f602f9 --- /dev/null +++ b/examples/src/main/scala/default.scala @@ -0,0 +1,24 @@ +package magnolia.examples + +import scala.language.existentials +import scala.language.higherKinds + +import magnolia._ +import scala.language.experimental.macros + +trait Default[T] { def default: T } + +object Default { + type Typeclass[T] = Default[T] + def join[T](ctx: JoinContext[Default, T]): Default[T] = new Default[T] { + def default = ctx.construct { param => param.typeclass.default } + } + + def dispatch[T](ctx: DispatchContext[Default, T])(): Default[T] = new Default[T] { + def default: T = ctx.subtypes.head.typeclass.default + } + + implicit val string: Default[String] = new Default[String] { def default = "" } + implicit val int: Default[Int] = new Default[Int] { def default = 0 } + implicit def generic[T]: Default[T] = macro Magnolia.generic[T] +} diff --git a/examples/src/main/scala/eq.scala b/examples/src/main/scala/eq.scala new file mode 100644 index 0000000..ec9be14 --- /dev/null +++ b/examples/src/main/scala/eq.scala @@ -0,0 +1,25 @@ +package magnolia.examples + +import scala.language.higherKinds + +import magnolia._ +import scala.language.experimental.macros + +trait Eq[T] { def equal(value: T, value2: T): Boolean } + +object Eq { + type Typeclass[T] = Eq[T] + def join[T](ctx: JoinContext[Eq, T]): Eq[T] = new Eq[T] { + def equal(value1: T, value2: T) = + ctx.parameters.forall { param => param.typeclass.equal(param.dereference(value1), param.dereference(value2)) } + } + + def dispatch[T](ctx: DispatchContext[Eq, T]): Eq[T] = new Eq[T] { + def equal(value1: T, value2: T): Boolean = + ctx.dispatch(value1) { case sub => sub.typeclass.equal(sub.cast(value1), sub.cast(value2)) } + } + + implicit val string: Eq[String] = new Eq[String] { def equal(v1: String, v2: String) = v1 == v2 } + implicit val int: Eq[Int] = new Eq[Int] { def equal(v1: Int, v2: Int) = v1 == v2 } + implicit def generic[T]: Eq[T] = macro Magnolia.generic[T] +} diff --git a/examples/src/main/scala/show.scala b/examples/src/main/scala/show.scala new file mode 100644 index 0000000..9af828e --- /dev/null +++ b/examples/src/main/scala/show.scala @@ -0,0 +1,26 @@ +package magnolia.examples + +import scala.language.existentials +import scala.language.higherKinds + +import magnolia._ +import scala.language.experimental.macros + +trait Show[Out, T] { def show(value: T): Out } + +object Show { + type Typeclass[T] = Show[String, T] + def join[T](ctx: JoinContext[Typeclass, T]): Show[String, T] = new Show[String, T] { + def show(value: T) = ctx.parameters.map { param => + s"${param.label}=${param.typeclass.show(param.dereference(value))}" + }.mkString(s"${ctx.typeName.split("\\.").last}(", ",", ")") + } + + def dispatch[T](ctx: DispatchContext[Typeclass, T]): Show[String, T] = new Show[String, T] { + def show(value: T): String = ctx.dispatch(value) { sub => sub.typeclass.show(sub.cast(value)) } + } + + implicit val string: Show[String, String] = new Show[String, String] { def show(s: String): String = s } + implicit val int: Show[String, Int] = new Show[String, Int] { def show(s: Int): String = s.toString } + implicit def generic[T]: Show[String, T] = macro Magnolia.generic[T] +} diff --git a/examples/src/main/scala/typeclasses.scala b/examples/src/main/scala/typeclasses.scala deleted file mode 100644 index aaa74fd..0000000 --- a/examples/src/main/scala/typeclasses.scala +++ /dev/null @@ -1,127 +0,0 @@ -package magnolia.examples - -import scala.collection.immutable.ListMap -import scala.language.existentials -import scala.language.higherKinds - -import magnolia._ -import scala.reflect._ -import scala.reflect.macros._ -import scala.language.experimental.macros -import scala.annotation.unchecked.uncheckedVariance - - -object Show { - type Typeclass[T] = Show[String, T] - def join[T](context: JoinContext[Typeclass, T]): Show[String, T] = new Show[String, T] { - def show(value: T) = context.parameters.map { param => - s"${param.label}=${param.typeclass.show(param.dereference(value))}" - }.mkString(s"${context.typeName.split("\\.").last}(", ",", ")") - } - - def dispatch[T](subclasses: Seq[Subclass[Typeclass, T]])(value: T): String = - subclasses.map { sub => sub.cast.andThen { value => - sub.typeclass.show(sub.cast(value)) - } }.reduce(_ orElse _)(value) - - implicit val string: Show[String, String] = identity - implicit val int: Show[String, Int] = new Show[String, Int] { def show(s: Int): String = s.toString } - implicit def generic[T]: Show[String, T] = macro Magnolia.generic[T] -} - -trait Show[Out, T] { def show(value: T): Out } - -object Eq { - type Typeclass[T] = Eq[T] - def join[T](context: JoinContext[Eq, T]): Eq[T] = new Eq[T] { - def equal(value1: T, value2: T) = - context.parameters.forall { param => param.typeclass.equal(param.dereference(value1), param.dereference(value2)) } - } - - def dispatch[T](subclasses: Seq[Subclass[Eq, T]]): Eq[T] = new Eq[T] { - def equal(value1: T, value2: T) = - subclasses.map { case subclass => - subclass.cast.andThen { value => subclass.typeclass.equal(subclass.cast(value1), subclass.cast(value2)) } - }.reduce(_ orElse _)(value1) - } - - implicit val string: Eq[String] = _ == _ - implicit val int: Eq[Int] = _ == _ - implicit def generic[T]: Eq[T] = macro Magnolia.generic[T] -} - -trait Eq[T] { def equal(value: T, value2: T): Boolean } - -object Default { - type Typeclass[T] = Default[T] - def join[T](context: JoinContext[Default, T]): Default[T] = new Default[T] { - def default = context.construct { param => param.typeclass.default } - } - - def dispatch[T](subclasses: Seq[Subclass[Default, T]])(): Default[T] = new Default[T] { - def default = subclasses.head.typeclass.default - } - - implicit val string: Default[String] = new Default[String] { def default = "" } - implicit val int: Default[Int] = new Default[Int] { def default = 0 } - implicit def generic[T]: Default[T] = macro Magnolia.generic[T] -} - -trait Default[T] { def default: T } - -object Decoder { - def join[T](context: JoinContext[Decoder, T])(value: String): T = - context.construct { param => param.typeclass.decode(value) } - - def dispatch[T](subclasses: Seq[Subclass[Decoder, T]])(param: String): T = - subclasses.map { subclass => - { case _ if decodes(subclass.typeclass, param) => subclass.typeclass.decode(param) }: PartialFunction[String, T] - }.reduce(_ orElse _)(param) - - def decodes[T](tc: Decoder[T], s: String): Boolean = try { tc.decode(s); true } catch { case e: Exception => false } - - implicit val string: Decoder[String] = new Decoder[String] { def decode(str: String): String = str } - implicit val int: Decoder[Int] = new Decoder[Int] { def decode(str: String): Int = str.toInt } - implicit def generic[T]: Decoder[T] = macro Magnolia.generic[T] -} - -trait Decoder[T] { def decode(str: String): T } - -sealed trait Tree[+T] -case class Leaf[+L](value: L) extends Tree[L] -case class Branch[+B](left: Tree[B], right: Tree[B]) extends Tree[B] - -sealed trait Entity - -case class Company(name: String) extends Entity -case class Person(name: String, age: Int) extends Entity -case class Address(line1: String, occupant: Person) - - - -sealed trait Alphabet - -case class Greek(άλφα: Letter, βήτα: Letter, γάμα: Letter, δέλτα: Letter, έψιλον: Letter, ζήτα: Letter, ήτα: Letter, θήτα: Letter) extends Alphabet -case class Cyrillic(б: Letter, в: Letter, г: Letter, д: Letter, ж: Letter, з: Letter) extends Alphabet -case class Latin(a: Letter, b: Letter, c: Letter, d: Letter, e: Letter, f: Letter, g: Letter, h: Letter, i: Letter, j: Letter, k: Letter, l: Letter, m: Letter) extends Alphabet - -case class Letter(name: String, phonetic: String) -case class Country(name: String, language: Language, leader: Person) -case class Language(name: String, code: String, alphabet: Alphabet) -//case class Person(name: String, dateOfBirth: Date) -case class Date(year: Int, month: Month, day: Int) - -sealed trait Month -case object Jan extends Month -case object Feb extends Month -case object Mar extends Month -case object Apr extends Month -case object May extends Month -case object Jun extends Month -case object Jul extends Month -case object Aug extends Month -case object Sep extends Month -case object Oct extends Month -case object Nov extends Month -case object Dec extends Month - |