Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zipper to iterate over list in Scala

Tags:

scala

zipper

This is a follow-up to my previous question. I can use an iterator, fold, zip, foreach and others to iterate over a list in Scala. Now I wonder if there are use cases where Zipper is most appropriate. Suppose I need read-only access without concurrency.

Could you give such an example and explain why the Zipper is the best choice?

like image 779
Michael Avatar asked Jun 01 '14 20:06

Michael


1 Answers

One of the many neat things about zippers is that they have a comonad instance, which allows us to solve a certain class of problems very elegantly.

Here's a quick example off the top of my head. Suppose that we've got a sequence of numbers and we want to do a simple form of smoothing with an exponential moving average, where the new value for each position in the list is an average of the current value and all the other values, but with more distant neighbors contributing less.

This isn't a terribly hard thing to compute imperatively, but if we use a zipper and a comonadic cobind it's not too far from a one-liner:

import scalaz._, Scalaz._

val weights = Stream.from(1).map(1.0 / math.pow(2, _))

def sumNeighborWeights(neighbors: Stream[Double]) =
  neighbors.fzipWith(weights)(_ * _).sum

def smooth(data: NonEmptyList[Double]) = data.toZipper.cobind { z =>
  (z.focus + sumNeighborWeights(z.lefts) + sumNeighborWeights(z.rights)) / 3
}

Now if we write:

val result = smooth(NonEmptyList[Double](0, 0, 0, 1, 0, 0, 0)).toList

We'll get the moral equivalent of:

List(1 / 24, 1 / 12, 1 / 6, 1 / 3, 1 / 6, 1 / 12, 1 / 24)

Which is what we want, given how we defined the problem.

like image 53
Travis Brown Avatar answered Oct 06 '22 07:10

Travis Brown