Uso de filtro sem forma, Scala
É fácil filtrarHList
em disforme por tipo:
val hlist = 1 :: 2 :: "3" :: true :: false :: HNil
hlist.filter[Int]
Mas como posso criar meu filtro de tipo personalizado? Eu quero o smth assim: por exemplo eu tenho lista de algumas funções:
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
Então, após o uso deste filtro, lista de funções do tipoString
para algum outro tipo será construído.
Eu tive uma idéia de usar o mapa para isso, mas não foi bem-sucedida.
EDIÇÃO
Mais informações sobre o meu comentário:
Eu tentei testar essas idéias no mapa:
Então, se eu tenho algumas listas (vamos operar comhlist
& 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
Muito interessante, porque compila e funciona incorretamente? Como eu acho que não é funciona, causa objeto não pode tomar tipo prameters de tal forma ...