1

In Scala I have a list of objects that represent points and contain x and y values. The list describes a path that goes through all these points sequentially. My question is how to use folding on that list in order to find the total length of the path? Or maybe there is even a better functional or Scala way to do this?

What I have came up with is this:

def distance = (0 /: wps)(Waypoint.distance(_, _)) 

but ofcourse this is totally wrong because distance returns Float, but accepts two Waypoint objects.

UPDATE:

Thanks for the proposed solutions! They are definitely interesting, but I think that this is too much functional for real-time calculations that may become heavy. So far I have came out with these lines:

val distances = for(i <- 0 until wps.size) yield wps(i).distanceTo(wps(i + 1))
val distance = (0f /: distances)(_ + _)

I feel this to be a fair imperative/functional mix that is both fast and also leaves the distances values between each waypoint for further possible references which is also a benifit in my case.

UPDATE 2: Actually, to determine, what is faster, I will have to do benchmarks of all the proposed solutions on all types of sequences.

6
  • Re: your update, there is nothing imperative about it, it's just as functional, but probably more expensive than the zipped.map solution. Feb 17, 2012 at 10:18
  • 1
    Be careful with wps(i), it might have O(n) complexity for some sequences, which will end up with O(n^2) final complexity. Feb 17, 2012 at 10:21
  • You are right. I sould be more careful when selecting the types of sequences. During the discussion on this question, I have switched to mutable Buffer type, but I will have to do benchmarks with possible sequence types and all kinds of proposed solutions. That will be a good experience!
    – noncom
    Feb 17, 2012 at 10:26
  • @Alan What can you say about speed of Buffer?
    – noncom
    Feb 17, 2012 at 11:46
  • 2
    Why are you folding to sum them? Just .sum it. Feb 17, 2012 at 14:21

4 Answers 4

4

This should work.

(wps, wps drop 1).zipped.map(Waypoint.distance).sum
3
  • Why not simply (wps zip wps.tail)? Also, looks like Waypoint.distance takes two arguments, not a tuple, so you need: map(Function.tupled(Waypoint.distance)) Feb 17, 2012 at 9:56
  • 1
    @TomaszNurkiewicz, (wps zip wps.tail) will throw an exception for empty lists. Feb 17, 2012 at 9:58
  • 2
    @TomaszNurkiewicz, and Tuple2#Zipped#map does not require a tupled function. Feb 17, 2012 at 9:59
3

Don't know if fold can be used here, but try this:

wps.sliding(2).map(segment => Waypoint.distance(segment(0), segment(1))).sum

wps.sliding(2) returns a list of all subsequent pairs. Or if you prefer pattern matching:

wps.sliding(2).collect{case start :: end :: Nil => Waypoint.distance(start, end)}.sum

BTW consider defining:

def distanceTo(to: Waypoint)

on Waypoint class directly, not on companion object as it looks more object-oriented and will allow you to write nice DSL-like code:

point1.distanceTo(point2)

or even:

point1 distanceTo point2

wps.sliding(2).collect{
  case start :: end :: Nil => start distanceTo end
}.sum
3
  • sum is a special application of fold.
    – ziggystar
    Feb 17, 2012 at 9:49
  • @ziggystar: true, but I am using sum to compute total of already calculated distances. I don't know how to use sum/foldLeft without intermediate sliding/map. Feb 17, 2012 at 9:52
  • Of course you still need sliding. You could use foldLeft and hold the last point inside the folding state. Though sliding looks nicer, I suspect (but is probably less efficient).
    – ziggystar
    Feb 17, 2012 at 9:53
1

Your comment "too much functional for real-time calculations that may become heavy" makes this interesting. Benchmarking and profiling are critical, since you don't want to write a bunch of hard-to-maintain code for the sake of performance, only to find out that it's not a performance critical part of your application in the first place! Or, even worse, find out that your performance optimizations makes things worse for your specific workload.

The best performing implementation will depend on your specifics (How long are the paths? How many cores are on the system?) But I think blending imperative and functional approaches may give you the worst-of-both worlds. You could lose out on both readability and performance if you're not careful!

I would very slightly modify missingfaktor's answer to allow you to have performance gains from parallel collections. The fact that simply adding .par could give you a tremendous performance boost demonstrates the power of sticking with functional programming!

def distancePar(wps: collection.GenSeq[Waypoint]): Double = {
  val parwps = wps.par
  parwps.zip(parwps drop 1).map(Function.tupled(distance)).sum
}

My guess is that this would work best if you have several of cores to throw at the problem, and wps tends to be somewhat long. If you have few cores or short paths, then parallelism will probably hurt more than it helps.

The other extreme would be a fully imperative solution. Writing imperative implementations of individual, performance critical, functions is usually acceptable, so long as you avoid shared mutable state. But once you get used to FP, you'll find this sort of function more difficult to write and maintain. And it's also not easy to parallelize.

def distanceImp(wps: collection.GenSeq[Waypoint]): Double = {
  if (wps.size <= 1) {
    0.0
  } else {
    var r = 0.0
    var here = wps.head
    var remaining = wps.tail

    while (!remaining.isEmpty) {
      r += distance(here, remaining.head)
      here = remaining.head
      remaining = remaining.tail
    }
    r
  }
}

Finally, if you're looking for a middle ground between FP and imperative, you might try recursion. I haven't profiled it, but my guess is that this will be roughly equivalent to the imperative solution in terms of performance.

def distanceRec(wps: collection.GenSeq[Waypoint]): Double = {
  @annotation.tailrec
  def helper(acc: Double, here: Waypoint, remaining: collection.GenSeq[Waypoint]): Double =
    if (remaining.isEmpty)
      acc
    else
      helper(acc + distance(here, remaining.head), remaining.head, remaining.tail)

  if (wps.size <= 1)
    0.0
  else
    helper(0.0, wps.head, wps.tail)
}
1

If you are doing indexing of any kind you want to be using Vector, not List:

scala> def timed(op: => Unit) = { val start = System.nanoTime; op; (System.nanoTime - start) / 1e9 }
timed: (op: => Unit)Double

scala> val l = List.fill(100000)(1)
scala> val v = Vector.fill(100000)(1)


scala> timed { var t = 0; for (i <- 0 until l.length - 1) yield t += l(i) + l(i + 1) }
res2: Double = 16.252194583

scala> timed { var t = 0; for (i <- 0 until v.length - 1) yield t += v(i) + v(i + 1) }
res3: Double = 0.047047654

ListBuffer offers fast appends, it doesn't offer fast random access.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.