Skip to content

Instantly share code, notes, and snippets.

@SystemFw
Last active June 7, 2019 05:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save SystemFw/a99a4099227042b7948cf944f06ecd01 to your computer and use it in GitHub Desktop.
Save SystemFw/a99a4099227042b7948cf944f06ecd01 to your computer and use it in GitHub Desktop.
Shapeless Poly with explicit arguments
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