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 6730321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:23:41+00:00 2026-05-26T10:23:41+00:00

If I have two lists val first = List(1, 2, 3) val second =

  • 0

If I have two lists

val first = List(1, 2, 3)
val second = List(4, 5, 6)

How would I obtain the following?

(1-4)^2 + (2-5)^2 + (3-6)^2
  • 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-05-26T10:23:42+00:00Added an answer on May 26, 2026 at 10:23 am

    zip, map and sum:

    first.view.zip(second).map(t => t._1 - t._2).map(x => x*x).sum
    
    • zip combine elements of the two list into a tuple
    • view is used to have the list computed lazily to not build a structure between the two map calls

    (edit to replace reduceLeft by sum)


    After seeing the comments, I feel I had to come back and explain about views. Basically a view turns a Traversable into an iterator like structure so that multiple intermediate structures don’t have to be created when apply methods like map, zip and a few others. The type members of GenIteratableViewLike gives a sense of what operations have special processing. So typically if you have a bunch of map, filter, drop, takeWhile applied in sequence, you can use view to gain some performance. The rule of thumb is to apply view early to minimize how many intermediate List are created and if necessary use force at the end to go back to List (or whatever collection you’re using). Thus Daniel’s suggestion.

    The thing about performance is that in practice if that’s important you sort of have to do a reality check. Here are some numbers (lower is better):

    no view List(62, 62, 62, 62, 63) sum: 311
    view before zip List(32, 31, 15, 16, 31) sum: 125
    view after zip List(31, 46, 46, 31, 31) sum: 185
    iterator List(16, 16, 16, 16, 15) sum: 79
    zipped List(62, 47, 62, 46, 47) sum: 264
    

    Code is here:

    import testing.Benchmark
    
    def lots[T](n: Int, f: => T): T = if (n > 0) { f; lots(n - 1, f) } else f
    
    def bench(n: Int, id: String)(block: => Unit) {
      val times = (new testing.Benchmark { 
        def run() = lots(10000, block)
      }).runBenchmark(n)
      println(id + " " + times + " sum: " + times.sum)
    }
    
    val first = List(1, 2, 3)
    val second = List(4, 5, 6)
    
    bench(5, "no view") { first.zip(second).map(t => t._1 - t._2).map(x => x*x).sum }
    bench(5, "view before zip") { first.view.zip(second).map(t => t._1 - t._2).map(x => x*x).sum }
    bench(5, "view after zip") { first.zip(second).view.map(t => t._1 - t._2).map(x => x*x).sum }
    bench(5, "iterator") { first.iterator.zip(second.iterator).map(t => t._1 - t._2).map(x => x*x).sum }
    bench(5, "zipped") { (first, second).zipped.map((a,b) => a - b).map(x => x*x).sum }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have two lists val L1 = List[(Int, Int)]((1,1), (2,2)) val L2 =
Suppose I have two lists: val a = List('a', 'b', 'c') val b =
I have two lists of objects. Each list is already sorted by a property
I have two lists, each on different tabs (sub-sites) of the site. I would
We have two lists: a=['1','2','3','4'] b=['2','3','4','5'] How to get a list with elements that
i have two lists: List<comparerobj> list_c = new List<comparerobj>(); List<comparerobj> list_b = new List<comparerobj>();
I am trying to merge two list in parallel. I have two sorted lists
Currently, I have two drop down lists that are populated with users' first and
I have two unsorted lists and I need to produce another list which is
I have two lists of objects, using Linq I would like to merge them,

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.