Created
March 14, 2019 20:41
-
-
Save qingwei91/84e869d7db68bb155819b68e09f3c92b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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