Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8764175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:57:58+00:00 2026-06-13T15:57:58+00:00

Ok so I thought this would be a snap, trying to practice Scala’s collection

  • 0

Ok so I thought this would be a snap, trying to practice Scala’s collection operators and my example is a list of points.

The class can calculate and return the distance to another point (as double).

However, fold left doesn’t seem to be the right solution – considering elements e1, e2, e3.. I need a moving window to calculate, I need the last element looked at to carry forward in the function – not just the sum

Sum {
  e1.dist(e2)
  e2.dist(e3)
  etc
} 

Reading the API I noticed a function called “sliding”, perhaps that’s the correct solution in conjunction with another operator. I know how to do this with loops of course, but trying to learn the scala way.

Thanks

import scala.math._

case class Point(x:Int, y:Int) {
  def dist(p:Point) = sqrt( (p.x-x)^2+(p.y-y)^2 )

}

object Point {

  //Unsure how to define this?
  def dist(l:Seq[Point]) =l.foldLeft(0.0)((sum:Double,p:Point)=>)

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T15:57:59+00:00Added an answer on June 13, 2026 at 3:57 pm

    If you want to do it as a fold, you can, but you need the accumulator to keep both the total and the previous element:

    l.foldLeft(l.head, 0.0){ 
      case ((prev, sum), p) => (p, sum + p.dist(prev)) 
    }._2
    

    You finish with a tuple consiting of the last element and sum, so use ._2 to get the sum part.

    btw, ^ on Int is bitwise logical XOR, not power. Use math.pow.

    The smartest way is probably using zipped, which is a kind of iterator so you don’t traverse the list more than once as you would using zip:

    (l, l.tail).zipped.map( _ dist _ ).sum
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay at first I thought this would be pretty straightforward. But I can't think
I thought this would be pretty academic but its not. I'm trying to traverse
I thought this would be easy to do but I can't find a way.
I thought this would be simple, but I can't see how to tell WCF
I thought this would be a relatively common thing to do, but I can't
I thought this would be really easy but I can't find a simple solution,
I thought this would be simple, but.... How can I get the backgroundcolor of
I thought this would be dead simple however.... Right, so all I'm simply trying
I thought this would be a JBoss FAQ, but I can't find it. I'd
I thought this would be a very common practice, but I am having a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.