You are calling repliesToComments()
recursively, but with the exact same arguments. This can only lead to problems.
Your recursive call is removing entries from the same list, and when it returns, those entries will still be gone. But because you loop over a copy of the list, your outer call won't be updated and try to remove comments the inner recursive call removed already.
Perhaps you wanted to call repliesToComments()
for a different set of comments when recursing? I suspect you meant to call it for comment.replies
instead?
Another problem your code has is that you ignore the return value of the recursive call:
botComments + repliesToComments(comments, author)
This adds the list botComments
and the return value of the recursive call, creating a new list, which you then drop to the floor by not assigning it to a variable. You probably want to use:
botComments += repliesToComments(comments, author)
instead.