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

  • Home
  • SEARCH
  • 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 6329195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:36:31+00:00 2026-05-24T17:36:31+00:00

I was looking at the way fold is defined for immutable.Set: def fold [A1

  • 0

I was looking at the way fold is defined for immutable.Set:

def fold [A1 >: A] (z: A1)(op: (A1, A1) ⇒ A1): A1 

yet foldLeft is defined as:

def foldLeft [B] (z: B)(op: (B, A) ⇒ B): B 

This looks weird for me, at least at first glance, since I was expecting fold to be able to change the type of the collection it returned, much like foldLeft does.

I imagine this is because foldLeft and foldRight guarantee something about the order in which the elements are folded. What is the guarantee given by fold?

  • 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-24T17:36:33+00:00Added an answer on May 24, 2026 at 5:36 pm

    When you’re applying foldLeft then your start value is combined with the first list element. The result is combined with the second list element. This result with the third and so on. Eventually, the list has collapsed to one element of the same type than your start value. Therefore you just need some type that can be combined by your function with a list element.
    For foldRight the same applies but in reverse order.

    fold does not guarantee the order in with the combinations are done. And it does not guarantee that it starts off at only one position. The folds might happen in parallel. Because you could have the parallelism it is required that any 2 list elements or return values can be combined – this adds a constraint to the types.

    Regarding your comment that you have to see a case were order has an effect: Assume you’re using folds to concatenate a list of characters and you want to have a text as result. If your input is A, B, C, you probably would like to preserve the order to receive ABC instead of ACB (for example).
    On the other hand if you’re, say, just adding up numbers, the order does not matter. Summing up 1, 2, 3 gives 6 independent of the additions’ order. In such cases using fold instead of foldLeft or foldRight could lead to faster execution.

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

Sidebar

Related Questions

I realize there's a way in Vim to hide/fold lines, but what I'm looking
I'm looking a way to build conditional assignments in bash: In Java it looks
First, I'm not looking a way to parse an RSS-feed. I am creating an
Im looking a way to do it with prototype, this js, needs to loads
I am looking a way to set AcceptMessagesOnlyFrom for a meetingroom (Mailbox). I am
looking a way to make a toggle on doubleclick, this is my code: $('#mydiv').toggle(function()
Im looking for way to integrate MsBuild scripts with Git source control. The first
I'm looking for way to PHP to detect if a script was run from
I'm looking a way to enable IP logging with log4net in ASP.NET. I found
I'm looking for way to write Javascript programs / scripts on desktop, not inside

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.