Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For Loop Array Declaration after recursive function def not compiling #365

Open
samer1977 opened this issue Sep 3, 2024 · 0 comments
Open

Comments

@samer1977
Copy link

samer1977 commented Sep 3, 2024

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)}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant