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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:07:27+00:00 2026-06-07T04:07:27+00:00

I am not sure what is the difference between fold and foldLeft in Scala.

  • 0

I am not sure what is the difference between fold and foldLeft in Scala.

The question Difference between fold and foldLeft or foldRight? has an answer that talks about ordering. That is understandable. But I still don’t understand why this works (from REPL):

scala> Array("1","2","3").foldLeft(0)(_ + _.toInt)
res6: Int = 6

but this does not:

scala> Array("1","2","3").fold(0)(_ + _.toInt)
<console>:8: error: value toInt is not a member of Any
              Array("1","2","3").fold(0)(_ + _.toInt)
                                               ^

What does this error message mean?

This line from the documentation is also confusing to me.

z – a neutral element for the fold operation; may be added to the
result an arbitrary number of times, and must not change the result
(e.g., Nil for list concatenation, 0 for addition, or 1 for
multiplication.)

Why will it be added an arbitrary number of times? I thought folding works differently.

  • 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-07T04:07:29+00:00Added an answer on June 7, 2026 at 4:07 am

    As defined by Scala, foldLeft is a linear operation while fold is allowed to be a tree operation. For example:

    List(1,2,3,4,5).foldLeft(0)(_ + _)
    // This is the only valid order of operations
    0+1 = 1
          1+2 = 3
                3+3 = 6
                      6+4 = 10
                            10 + 5 = 15
                                     15  // done
    
    List(1,2,3,4,5).fold(0)(_ + _)
    // This is valid
    0+1 = 1             0+3 = 3           0+5 = 5
          1+2 = 3             3+4 = 7           5
                3         +         7=10        5
                                      10    +   5 = 15
                                                    15  // done
    

    In order to allow arbitrary tree decompositions of a sequential list, you must have a zero that doesn’t do anything (so you can add it wherever you need it in the tree) and you must create the same sort of thing that you take as your binary arguments so the types don’t change on you depending on how you decompose the tree.

    (Being able to evaluate as a tree is nice for parallelization. If you want to be able to transform your output time as you go, you need both a combination operator and a standard start-value-transforms-sequence-element-to-desired-type function just like foldLeft has. Scala has this and calls it aggregate, but in some ways this is more like foldLeft than fold is.)

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

Sidebar

Related Questions

Just like in the question, not sure what the difference between '' and N''
I am not really sure about the difference between CDF (Cumulative Distribution Function) and
Ok, so I'm not quite sure the difference between these languages. Could someone clarify?
I am not sure what is the difference between {% url url_name %} and
I'm not sure I understand the difference between an if block and switch/select block.
I am not sure what the difference is between specifying Unit as the return
I used this gem in my application, but I'm not sure the difference between
What is the difference between the two as I am not sure. This is
I'm not sure I understand if there is a difference between a finite state
What's the difference between x:Key and x:Name in WPF? I am not sure what

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.