Skip to content

Instantly share code, notes, and snippets.

@d6y
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d6y/de4664f2a03d6fad1730 to your computer and use it in GitHub Desktop.
Save d6y/de4664f2a03d6fad1730 to your computer and use it in GitHub Desktop.
Exploring everywhere
import shapeless._, poly._
object SYBMain extends App {
// Play JSON boils down to:
sealed trait JsValue
case object JsNull extends JsValue
case class JsObject (fields: Seq[(String, JsValue)]) extends JsValue
case class JsNumber (value : BigDecimal) extends JsValue
case class JsArray (value : Seq[JsValue] = List()) extends JsValue
case class JsString (value : String) extends JsValue
case class JsBoolean (value : Boolean) extends JsValue
val tree : JsValue =
JsObject(Seq(
("cat", JsNumber(1)),
("dog", JsNumber(2)),
("fox", JsNull)
)
)
object fix extends Poly1 {
implicit def bigger = at[BigDecimal](_ * 987654321)
}
println(
everywhere(fix)(tree)
)
// Result:
// JsObject(List((cat,JsNumber(1)), (dog,JsNumber(2)), (fox,JsNull)))
// Desired (bigger applied):
// JsObject(List((cat,JsNumber(987654321)), (dog,JsNumber(1975308642)), (fox,JsNull)))
// This is exploring.
// The true aim is to be able to write something to remove (_,JsNull)
// that occurs anywhere in a JsValue tree.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment