Uso del filtro en sin forma, Scala
Es fácil de filtrarHList
en forma sin forma por tipo:
val hlist = 1 :: 2 :: "3" :: true :: false :: HNil
hlist.filter[Int]
Pero, ¿cómo puedo hacer mi filtro de tipo personalizado? Quiero algo así: por ejemplo, tengo una lista de algunas funciones:
def function1(s: String) = s.toInt
def function2(s: String) = s.toDouble
def function3(i: Int) = i.toDouble
val hflist = function1 _ :: function3 _ :: function2 _ :: HNil
hflist customFilter[String] //> function1 _ :: function2 _ :: HNil
Así que después de usar este filtro, lista de funciones de tipoString
Para algún otro tipo se construirá.
Tuve una idea de usar el mapa para esto, pero no tuvo éxito.
EDICIÓN
Más información sobre mi comentario:
Intenté probar estas ideas en el mapa:
Así que si tengo algunas listas (vamos a operar conhlist
& hflist
):
object allFunction extends Poly1 {
implicit def default[T, M] =
at[T => M](t => {
object grabStringFunc extends skip {
implicit def stringFunc[A] = at[T => A](_ :: HNil)
}
println(hflist flatMap grabStringFunc) //> here we should see result, list of functions
})
hlist map allFunction
//> result of this should be smth like (types)
//> shapeless.::[Int => Double,shapeless.HNil]]
//> shapeless.::[Int => Double,shapeless.HNil]]
//> shapeless.::[String => Int,shapeless.::[String => Double,shapeless.HNil]]
//> shapeless.HNil
//> shapeless.HNil
Muy interesante, ¿por qué compila y funciona incorrectamente? Como creo que no funciona, el objeto no puede tomar parámetros de tipo de tal manera ...