Zastosuj filtr w bezkształtnej, Scali
Jest łatwy do filtrowaniaHList
w bezkształtnym typie:
val hlist = 1 :: 2 :: "3" :: true :: false :: HNil
hlist.filter[Int]
Ale jak mogę zrobić filtr niestandardowy? Chcę coś takiego: na przykład mam listę niektórych funkcji:
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
Więc po użyciu tego filtra, lista funkcji z typuString
do innego typu zostanie skonstruowany.
Miałem pomysł, aby użyć do tego mapy, ale nie udało się.
WYDANIE
Więcej informacji o moim komentarzu:
Próbowałem przetestować te pomysły na mapie:
Więc jeśli mam kilka list (działajmy zhlist
& 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
Bardzo interesujące, dlaczego się kompiluje i działa niepoprawnie? Jak myślę, że to nie działa, ponieważ obiekt nie może przyjmować parametrów typu w taki sposób ...