As a quick note to self, I wrote this Scala code as a way to (a) find the first element in a sequence, and then (b) return that element without traversing the rest of the sequence.
I initially thought about writing this code with a while
loop, for
loop, or for
expression — because I knew I needed a loop and a way to break out of a loop — but then I realized that an iterator would help me out here.
Also, please note that there is a potentially-better solution after this — the one that uses the “view.”
A first solution
So without any further ado, here’s this solution: