Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RollingSum #216

Open
johnynek opened this issue Oct 11, 2013 · 2 comments
Open

RollingSum #216

johnynek opened this issue Oct 11, 2013 · 2 comments
Labels

Comments

@johnynek
Copy link
Collaborator

Awful mutability....

    def finish[V:Semigroup](in: Iterator[V]): Iterator[(Option[V], V)] = {
      var prev: Option[V] = None
      in.map { v =>
        val stored = prev
        prev = Some(prev.map(Semigroup.plus(_, v)).getOrElse(v))
        (stored, v)
      }
    }
@ryanlecompte
Copy link

Don't feel badly about the var... Iterator's scanLeft (which returns another lazy Iterator) also uses the same var approach! :) https://github.com/scala/scala/blob/master/src/library/scala/collection/Iterator.scala#L487

@sritchie
Copy link
Collaborator

@johnynek where do you want to use this? What's the issue? I guess the task is to add this to the Semigroup object?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants