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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:13:21+00:00 2026-05-12T20:13:21+00:00

scala> val l = List((1,2), (2,3)) l: List[(Int, Int)] = List((1,2), (2,3)) I can

  • 0
scala> val l = List((1,2), (2,3))
l: List[(Int, Int)] = List((1,2), (2,3))

I can do

scala>  (0 /: l) {(a, i) => i._1 + a}
res20: Int = 3

But I want to be able to name the tuple’s elements. Something like:

scala> (0 /: l) {(a, (b,c)) => b + a}
<console>:1: error: not a legal formal parameter
       (0 /: l) {(a, (b,c)) => b + a}
                     ^

I know I can do:

scala> (0 /: l) {(a, i) => val (b, c) = i; b + a}
res0: Int = 3

But is there a way of making the code more concise (my real code involves several such folds and I don’t like needing to invent a variable name (‘i’ in the example above) for each)

  • 1 1 Answer
  • 3 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-12T20:13:21+00:00Added an answer on May 12, 2026 at 8:13 pm

    Give this a try:

    (0 /: l) { case (a, (b, c)) => b + a }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I want to add functionality like map to a Scala List , something
scala> val alist = List(1,2,3,4,5) alist: List[Int] = List(1, 2, 3, 4, 5) scala>
I had a List of Scala tuples like the following: val l = List((1,2),(2,3),(3,4))
Given the following Scala List: val l = List(List(a1, b1, c1), List(a2, b2, c2),
Here's a seq of tuples in Scala val t = Seq((1,2,3),(4,5,6)) I like to
scala> val m = Map(1 -> 2) m: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2) scala>
scala> class A (s: String*) { val l: ListBuffer[String] = ListBuffer[String](s) } <console>:8: error:
I had a Scala class as follows: class ClassA(val name: String) { println(this is
scala> import scala.collection.mutable.Buffer import scala.collection.mutable.Buffer scala> val b = Buffer(1, 2, 3) b: scala.collection.mutable.Buffer[Int]
import java.util.Random class Kostka { val rand = new Random(System.currentTimeMillis()) val value: List[Int] =

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.