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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:09:10+00:00 2026-06-01T12:09:10+00:00

can someone explain the best way to get around the following, rather curious type

  • 0

can someone explain the best way to get around the following,
rather curious type error. Suppose I create a list of tuples like so:

scala> val ys = List((1,2), (3,4), (5,6))
ys: List[(Int, Int)] = List((1,2), (3,4), (5,6))

Now, if I want to map this to a List(Int)

scala> ys.map((a: Int, b: Int) => a + b)
<console>:9: error: type mismatch;
found   : (Int, Int) => Int
required: ((Int, Int)) => ?
   ys.map((a: Int, b: Int) => a + b)
                           ^

Any clues? I know I can use the for comprehension

scala> for ((a, b) <- ys) yield a + b
res1: List[Int] = List(3, 7, 11)

But it feels wrong to bust out a comprehension in this setting. Thanks!

  • 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-01T12:09:12+00:00Added an answer on June 1, 2026 at 12:09 pm

    try:

    ys.map { case (a: Int, b: Int) => a + b }
    

    or:

    ys.map(p: (Int, Int) => p._1 + p._2)
    

    What’s happening is that ys is a List of (Int,Int), so map expects a function from a single argument, which happens to be a tuple (Int,Int), to something else (technically, map expects an argument of Function1[(Int,Int),Int]. The function (a: Int, b: Int) => a+b is not actually a function from a single argument (Int, Int) to Int; instead it’s a function of two arguments, both Ints, to an Int (a Function2[Int,Int,Int]). The difference is subtle, but important since Scala makes a distinction:

    val f: Function1[(Int,Int),Int] = (p: (Int,Int)) => p._1 + p._2
    ys.map(f) // fine
    
    val g: Function1[(Int,Int),Int] = { case (a: Int, b: Int) => a + b }
    ys.map(g) // fine, technically a PartialFunction[(Int,Int),Int]
    
    val h: Function2[Int,Int,Int] = (a: Int, b: Int) => a + b
    ys.map(h) // ERROR!
    

    To explain my suggestions at the top of the answer: In the first example, we have changed the definition of the function given to map to use case, which tells Scala to unpack the single (Int,Int) argument into its two parts. (Note also the use of curly braces instead of parentheses.) In the second example, we have a function of a single tuple argument, p, and we manually extract each part of the tuple.

    Finally, note that you don’t need the type annotations either. These work just as well:

    ys.map { case (a,b) => a + b }
    
    ys.map(p => p._1 + p._2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone explain the best way to handle this approach. I am using TinyMCE
Could someone explain the best way to achieve the following, I think it's a
Can someone explain why it's best use is for prototyping? Should it not be
Can someone explain the threading model in windows metro? I really get confusion about
I can't quite get my head around this idea, wonder if someone could help
Can someone please explain in a way as simple as possible what the Model
Can someone explain this simple, yet deceiving, anomaly? There are two models, where B
Can someone explain me why this works: select val1, val2, count(case when table2.someID in
Can someone explain why, in this situation, scandir is getting my directory, but the
Can someone explain step by step how this query is processed? SELECT F.ID, F.FirstName,

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.