Created
December 22, 2014 20:12
-
-
Save johnynek/a0c8d2e747e02a9a6397 to your computer and use it in GitHub Desktop.
Why doesn't scala give scanLeft on TraversableOnce?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This won't work because of the "situation" with scala collections. TraversableOnce actually | |
* needs like 12 methods implemented. | |
*/ | |
class ScanLeft[A, B](it: TraversableOnce[A], init: B, fn: (B, A) => B) extends TraversableOnce[B] { | |
override def foreach[U](effect: B => U): Unit = { | |
var bstate = init | |
effect(bstate) | |
it.foreach { a: A => | |
bstate = fn(bstate, a) | |
effect(bstate) | |
} | |
} | |
} | |
viktorklang
commented
Dec 22, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment