foo(name, u) } } def foo(name:-6ren">
gpt4 book ai didi

scala - 部分应用的递归函数

转载 作者:行者123 更新时间:2023-12-01 07:31:31 24 4
gpt4 key购买 nike

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
}

如何将 foo 编写为部分应用的函数,我不必在每次递归调用中传递名称而只需调用 foo(map1)

最佳答案

两种选择:

def foo(name:String)(map:Map): Unit = {
val bar = foo(name)_
//match case...
// case 1:
bar(x)

// case 2:
bar(y)
}

或者:

def foo(name:String): Map => Unit = {
def bar(map: Map): Unit = {
//match case...
// case 1:
bar(x)

// case 2:
bar(y)
}
bar
}

关于scala - 部分应用的递归函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3182783/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com