Skip to content

Instantly share code, notes, and snippets.

@qingwei91
Created March 14, 2019 20:41
Show Gist options
  • Select an option

  • Save qingwei91/84e869d7db68bb155819b68e09f3c92b to your computer and use it in GitHub Desktop.

Select an option

Save qingwei91/84e869d7db68bb155819b68e09f3c92b to your computer and use it in GitHub Desktop.
val nestedQuery = queryPath(
"oh",
queryPath(
"my",
queryString
)
)
// a trick to fold into String, this is interesting as it shows that
// generalized type constructor is super powerful; it can represent a
// more specialized type easily
type JustString[A] = String
// important part: convert each layer of query into a string
val print: HAlgebra[QueryF, JustString] = new HAlgebra[QueryF, JustString] {
override def apply[A](fa: QueryF[JustString, A]): JustString[A] = {
fa match {
case QueryStringF => "as[String]"
case QueryBoolF => "as[Bool]"
case q: QueryPathF[JustString, A] => s"${q.path}.${q.next}"
}
}
}
hCata(print, nestedQuery)
// res44: JustString[String] = "oh.my.as[String]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment