Dataweave中的尾递归 [英] Tail Recursion in Dataweave
问题描述
有没有办法采用递归函数(如下所示)并使它尾递归?我有这样的输入:
Is there a way to take a recursive function (like the following) and make it tail recursive? I have an input like this:
{
"message": "Test ",
"read": [
{
"test": " t "
}
]
}
和此Dataweave函数
and this Dataweave function
fun trimWS(item) = item match {
case is Array -> $ map trimWS($)
case is Object -> $ mapObject {
($$): $ match {
case is String -> trim($)
case is Object -> trimWS($)
case is Array -> $ map trimWS($)
else -> $
}
}
case is String -> trim($)
else -> $
}
推荐答案
我对您现有的功能做了一些修改,以简化它,并且我还在Mule 4.2.1下运行了一些测试.
I reworked a little bit your existing function to simplify it and I also run a few tests under Mule 4.2.1.
通过构建深度超过840层的数据结构,我能够导航和修剪字段.我的猜测是由于数据的结构和惰性评估,我能够获得超过256深度的深度,这是DW 2.0抛出StackOverflow时的默认值.
By building a data structure with over 840 levels deep, I was able to navigate and trim the fields. My guess is because of the structure of the data and lazy evaluation I am able to get past 256 depths which is the default value where DW 2.0 is throwing StackOverflow.
您还可以通过传递运行时参数来增加默认值,该参数的名称为 com.mulesoft.dw.stacksize
(例如, com.mulesoft.dw.stacksize = 500
>)或您的系统可以处理的其他任何数字.
You can also increase the default value by passing a runtime parameter, its name is com.mulesoft.dw.stacksize
(e.g. com.mulesoft.dw.stacksize=500
) or any other number provided your system can handle it.
正如我说的那样,创建尾递归版本并不容易,它会使代码复杂化,与现有版本相比,它的可维护性较差,等等.
As I said creating a tail-recursive version is not easy, it will complicate the code, it will be way less maintainable as compared to the existing version, etc.
即使我没有直接回答你的问题,我希望它会有所帮助.
I hope it helps even if I am not directly answering your question.
%dw 2.0
output application/json
var ds = {
"message": "Test ",
"read": [
{
"test": " t "
}
]
}
var deepData = (0 to 840) as Array reduce (e, acc=ds) -> {value: " TO_TRIM ",next: acc}
fun trimWS(item) = item match {
case is Array -> $ map trimWS($)
case is Object -> $ mapObject {($$): trimWS($)}
case is String -> trim($)
else -> $
}
---
trimWS(deepData)
这篇关于Dataweave中的尾递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!