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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:52:53+00:00 2026-06-02T20:52:53+00:00

The context for this is pretty simple. My assumptions are based on Odersky’s book

  • 0

The context for this is pretty simple. My assumptions are based on Odersky’s book “Programming in Scala, 2nd Edition”, section 8.5 describing “Placeholder Syntax”.

I have a List[List[Boolean]] (i.e. a rectangular bit map) where I am attempting to count the total occurrences of the value “true”. Here’s the REPL line defining the data which executes fine:

val rowsByColumns =
    List(   List(false, true, false)
          , List(true, true, true)
          , List(false, true, false)
        )

Next, I attempted to count the occurrences of “true” with the following line. And instead of executing, I receive an error:

val marks = (for(row <- rowsByColumns)
    yield {row.foldLeft[Int](0)(_ + (if (_) 1 else 0))}).sum

<console>:8: error: wrong number of parameters; expected = 2
       val marks = (for(row <- rowsByColumns) yield {row.foldLeft[Int](0)(_ + (i
f (_) 1 else 0))}).sum
                                                                        ^

I did not understand the error as I have the two underscores representing the parameters to the function. So, I made the function more explicit by writing this which executes just fine:

val marks = (for(row <- rowsByColumns)
      yield {row.foldLeft[Int](0)((sum, marked) => sum + (if (marked) 1 else 0))}
    ).sum

My question is this: Why did I receive and error for the less explicit case, but when I map out the function by reducing the “simplifications”, it executes correctly?

Thank you for any insight you can give me on this.

  • 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-02T20:52:55+00:00Added an answer on June 2, 2026 at 8:52 pm

    The limitations of Scala’s placeholder syntax for anonymous functions can be extremely confusing (to me, at least). One rule of thumb is that underscores get bound to their nearest enclosing parentheses, but this is an approximation—see section 6.23 of the Scala specification for the detail:

    An expression e of syntactic category Expr binds an underscore
    section u, if the following two conditions hold: (1) e properly
    contains u, and (2) there is no other expression of syntactic
    category Expr which is properly contained in e and which itself
    properly contains u.

    In this case the compiler doesn’t see the second underscore as a second parameter. This might seem odd, since _ + _ is properly seen as having two parameters, and if (_) x else y is equivalent to z => if (z) x else y (where z is a fresh identifier), but nesting the two doesn’t work.

    It’s true that the compiler could in theory figure out that the two underscores should be parameters for the same anonymous function in your foldLeft, but not, for example, in the following, where the second underscore really does need to be bound separately:

    rowsByColumns.map(_.map(!_))
    

    This would require a lot of extra cleverness on the part of the compiler, though, and the Scala language designers have decided that it’s not worth it—that placeholder syntax only needs to be provided for some fairly simple cases without nested expressions.


    Luckily in this case you can just write rowsByColumns.flatten.count(identity) instead. flatten here concatenates the sublists to give a single List[Boolean]. We then want to know how many of the values in that list are true. count takes a predicate and tells you how many values in a collection satisfy that predicate. For example, here’s one way to count the even numbers between 1 and 10 (inclusive):

    val isEven: Int => Boolean = _ % 2 == 0    
    (1 to 10) count isEven
    

    In your case, though, we already have boolean values, so the predicate doesn’t need to do any work—it can just be the identity function x => x. As dhg notes in a comment, Scala’s Predef object provides this as a method named identity, which I’m using here. You could just as easily write rowsByColumns.flatten.count(x => x), though, if you find that clearer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm drawing simple text in HTML5 canvas using this: context.fillStyle = #FF0000 context.font =
Pretty simple code but a weird situation... have a template with this snippet: {%
So I'm trying to add a pretty simple context menu like the example given
New to entity framework, but I would think this should be pretty simple. My
I reproduced this issue in a pretty simple .NET 4.0 test project. Using MVVM,
This problem is pretty simple, I think. The thing is: My TextView (Which is
This one should be pretty simple. I am making a non-MVC ASP.NET 2.0 site.
This is a pretty simple problem. Inserting data into the table normally works fine,
This is a pretty general question, but the specific thing I'm doing is simple
This should be pretty simple, yet it's blowing up. Any ideas? d = BigDecimal.new(2.0)

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.