Last active
June 7, 2019 05:14
-
-
Save SystemFw/a99a4099227042b7948cf944f06ecd01 to your computer and use it in GitHub Desktop.
Shapeless Poly with explicit arguments
This file contains 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
object Args { | |
// it requires the cats, shapeless, and kittens libraries | |
import cats._, implicits._ | |
import cats.sequence._ | |
import shapeless._, labelled._ | |
import shapeless.syntax.singleton._ | |
// Gitter question: | |
// it masks fields of a record according to an explicit argument | |
// if the contenct of each field is in the ``fields`` set, convert it to `None` | |
// if not, wrap them in Some | |
object FilterNot extends Poly1 { | |
implicit def caseField[K <: Symbol, A](implicit key: Witness.Aux[K]) = | |
at[FieldType[K, A]] { x => (fields: Set[String]) => | |
field[K] { | |
if (!fields.contains(key.value.name)) Some(x) | |
else None | |
} | |
} | |
} | |
val hlist = 'id ->> 1 :: 'name ->> "John" :: HNil | |
val fields = Set("name") | |
val b = hlist.traverse(FilterNot).apply(fields) // Some(1) :: None :: HNil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment