Partiellement appliqué les fonctions récursives
-
02-10-2019 - |
Question
def mainCaller() = {
val name = "xyz"
someList.foreach { u:Map => foo(name, u) }
}
def foo(name:String)(map:Map): Unit = {
//match case....
//recursive call to foo in each case where name remains same, but map changes
}
Comment puis-je écrire foo en fonction partiellement appliquée, où je ne dois passer le nom dans chaque appel récursif et juste foo(map1)
d'appel?
La solution
Deux options:
def foo(name:String)(map:Map): Unit = {
val bar = foo(name)_
//match case...
// case 1:
bar(x)
// case 2:
bar(y)
}
Ou:
def foo(name:String): Map => Unit = {
def bar(map: Map): Unit = {
//match case...
// case 1:
bar(x)
// case 2:
bar(y)
}
bar
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow