3 ms·
While I'm not a huge fan of functional programming, this can be done pretty easily with a fold. For example in Scala: xs.foldLeft(""){ (prev, cur) => if (pre
by malisper 10y ago
While I'm not a huge fan of functional programming, this can be done pretty easily with a fold. For example in Scala:
xs.foldLeft(""){ (prev, cur) => if (prev != "a" && cur != "a") parseLine(cur); cur}
Although the result is pretty and shorter than your solution, it took me quite a while to figure out how to do it. Also, your code has a bug. You are skipping two lines after every "a". The code should be i += 1 instead of i += 2. <Insert lame joke about mutation causing bugs here.>
- hskl 10y agoHere's your version translated to C#: xs.Aggregate("", (prev, cur) => { if (prev != "a" && cur != "a") parseLine(cur); return cur; }); Working demo: https://repl.it/EwaE https://repl.it/EwaE
- dispose13432 10y ago>Also, your code has a bug Oops. You're right. Got spoiled by foreach loops lately :)