Skip to content

Instantly share code, notes, and snippets.

View mandubian's full-sized avatar

Pascal Voitot mandubian

View GitHub Profile
@mandubian
mandubian / CODE.scala
Last active February 27, 2018 15:43 — forked from playxamplez-admin/CODE
#Recursive class #Json #Reads using #lazyRead feature #Play21 #scala A class inheriting a parent trait with a member of the parent type
sealed abstract trait Tree
case class Leaf(a: String) extends Tree
case class Node(l: Tree, r: Tree) extends Tree
implicit val treeR: Reads[Tree] =
__.read[Leaf].map(x => x:Tree) orElse
(
(__ \ "l").lazyRead(treeR) and (__ \ "r").lazyRead(treeR)
)(Node.apply _).map(x => x:Tree)
@mandubian
mandubian / Macros.scala
Last active December 20, 2015 00:59 — forked from scalamacroxamplez-admin/CODE
#StructuralType without #reflection using #annotation trick imagined by @xeno_by (Eugene Burmako) originally https://gist.github.com/xeno-by/5967900 compatible #quasiquote
import scala.annotation.StaticAnnotation
import scala.reflect.macros.Macro
import language.experimental.macros
class body(tree: Any) extends StaticAnnotation
trait Macros extends Macro {
import c.universe._
def selFieldImpl = {
@mandubian
mandubian / CODE
Created March 5, 2013 17:02 — forked from zxamplez/CODE
<put your code here>
@mandubian
mandubian / CODE.scala
Last active December 14, 2015 09:29 — forked from zxamplez/CODE
#Play2.1 #Json Read 2 fields exclusively (accepts only one present) essaimodif #scala #nullable
import play.api.libs.json._
import play.api.libs.functional.syntax._
val r = (
(__ \ "field1").readNullable[String] and
(__ \ "field2").readNullable[Int]
).tupled.filter(ValidationError("unexpected result")){
case( Some(x), None ) => true;
case ( None, Some(x) ) => true;
case _ => false
@mandubian
mandubian / CODE.scala
Last active December 14, 2015 09:29 — forked from zxamplez/CODE
Workaround to create a #Play2.1 #Json #Reads / #Writes in a companion object #scala (in #Play2.2, it's no more needed)
case class Foo(bar: String)
// extends Function1 is just to workaround a limitation in Play2.1
object Foo extends Function1(bar: String, Foo) {
implicit val fmt = Json.format[Foo]
}
@mandubian
mandubian / CODE
Created March 1, 2013 15:25 — forked from zxamplez/CODE
a
<put your code here>
@mandubian
mandubian / CODE.scala
Last active December 14, 2015 09:29 — forked from zxamplez/CODE
#JSON Read a value and if failure, set default value #scala #Play2.1
import play.api.libs.json._
val reads = (__ \ "foo").read[String] orElse Reads.pure("default string")
@mandubian
mandubian / code.scala
Last active December 14, 2015 09:18 — forked from gre/code
test
@mandubian
mandubian / answer.scala
Last active December 11, 2015 17:18
c'est une description
mon implementation à moi qui tue!