-
Notifications
You must be signed in to change notification settings - Fork 123
Description
Hi,
I know the title sounds confusing but hopefully my explanation will be clearer. This is based on this post which I resolved. However I had a second thought and I figured that this would be better done using group-by function! Its not going to save us much lines of code but it would probably look cleaner.
Going through the exercise I encountered strange error. lets assume for now that the end result will be an array of of objects, here is the spec which wont compile:
import "http://jslt.schibsted.com/2018/experimental" as exp
def collapse-objects(array)
if(not(is-array($array)))
$array
else if(size($array)==1)
$array[0]
else
$array[-1]+collapse-objects($array[0 : -1])
[for (exp:group-by(., .name, {.date:.height})) {.key: collapse-objects(.values)} ]
Running the above will give me the scariest error of all :
com.schibsted.spt.data.jslt.JsltException: Parse error: Encountered " "for" "for "" at line 10, column 2 ....
...
However if I store the array def in a variable and return the variable as in the below it works!!
let result = [for (exp:group-by(., .name, {.date:.height})) {.key: collapse-objects(.values)} ]
$result
My question why is that? am I missing something or is the parser not able to tell the end of the function from what is being returned?
By the way if I change the result from being an array to complex object - which is what is expected in the post - it works too without having to declare a variable !!
{for (exp:group-by(., .name, {.date:.height})) .key: collapse-objects(.values)}