3 ms·
def parsedLines = lines.withIndex().filter{l, i -> i < 1 || lines[i - 1] != 'a'}.collect{parseLine(it)} Do you really think that the imperative version is simp
by tigershark 10y ago
def parsedLines = lines.withIndex().filter{l, i -> i < 1 || lines[i - 1] != 'a'}.collect{parseLine(it)}
Do you really think that the imperative version is simpler that this small groovy one liner?
I strongly disagree.
And probably in a more functional language than groovy you can do even better.
And btw you are not doing anything with the result of parseLine, while I am saving it in a new collection of parsedLines.
- vorg 10y ago> def parsedLines = lines.withIndex().filter{l, i -> i < 1 || lines[i - 1] != 'a'}.collect{parseLine(it)} In that Apache Groovy sample, you put an `l` as the first parameter to the `filter` but don't use it anywhere, which is not simple for people reading it. The logic instead requires you to refer to the global input `lines`, which is better expressed imperatively. And btw in Groovy that code would probably only work on ASCII data.
- tigershark 10y agoReally is it so difficult to ignore one parameter for people reading that one liner? I could have used the underscore but honestly when I'm writing from the phone I don't usually care for small things that don't change absolutely nothing. At least my code doesn't have the bug that skips TWO lines after the a, like the original code, and it actually saves the parsed lines in a new collection rather than throwing them away and doing nothing at all apart from burning cpu. But for some strange reason you still prefer that obscure bug-ridden and useless imperative code rather than the self explaining and declarative functional version. Enjoy your imperative bugs :)
- vorg 10y agoAny code that's not tested has bugs. As for reading code, when I read yours I can't work out why you put .filter{l, i -> i < 1 || lines[i - 1] != 'a'} instead of .filter{l, i -> i < 1 || lines[i - 1][0] != 'a'} to skip the line after one that begins with an 'a', or .filter{l, i -> i < 1 || lines[i - 1] != "a"} to skip the line after one that is an "a". I'd need to go run it to work out what it does.