Slide 82
Slide 82 text
Word count
performance
txt.foldLeft((0, true)) {
case ((wc, _), ' ') => (wc, true)
case ((wc, true), x) => (wc + 1, false)
case ((wc, false), x) => (wc, false)
}
new ParString(txt).aggregate((0, 0, 0))({
case ((ls, 0, _), ' ') => (ls + 1, 0, ls + 1)
case ((ls, 0, _), c) => (ls, 1, 0)
case ((ls, wc, rs), ' ') => (ls, wc, rs + 1)
case ((ls, wc, 0), c) => (ls, wc, 0)
case ((ls, wc, rs), c) => (ls, wc + 1, 0)
}, {
case ((0, 0, 0), res) => res
case (res, (0, 0, 0)) => res
case ((lls, lwc, 0), (0, rwc, rrs)) =>
(lls, lwc + rwc - 1, rrs)
case ((lls, lwc, _), (_, rwc, rrs)) =>
(lls, lwc + rwc, rrs)
})
100 ms
cores: 1 2 4
time: 137 ms 70 ms 35 ms