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

The Archive Base Latest Questions

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

I read in Programming in Scala section 23.5 that map, flatMap and filter operations

  • 0

I read in Programming in Scala section 23.5 that map, flatMap and filter operations can always be converted into for-comprehensions and vice-versa.

We’re given the following equivalence:

def map[A, B](xs: List[A], f: A => B): List[B] =
  for (x <- xs) yield f(x)

I have a value calculated from a series of map operations:

val r = (1 to 100).map{ i => (1 to 100).map{i % _ == 0} }
                  .map{ _.foldLeft(false)(_^_) }
                  .map{ case true => "open"; case _ => "closed" }

I’m wondering what this would look like as a for-comprehension. How do I translate it?

(If it’s helpful, in words this is:

  • take integers from 1 to 100
  • for each, create a list of 100 boolean values
  • fold each list with an XOR operator, back into a boolean
  • yield a list of 100 Strings “open” or “closed” depending on the boolean

I imagine there is a standard way to translate map operations and the details of the actual functions in them is not important. I could be wrong though.)

  • 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-24T06:58:31+00:00Added an answer on May 24, 2026 at 6:58 am

    Is this the kind of translation you’re looking for?

    for (i <- 1 to 100;
         val x = (1 to 100).map(i % _ == 0);
         val y = x.foldLeft(false)(_^_);
         val z = y match { case true => "open"; case _ => "closed" })
      yield z
    

    If desired, the map in the definition of x could also be translated to an “inner” for-comprehension.

    In retrospect, a series of chained map calls is sort of trivial, in that you could equivalently call map once with composed functions:

     s.map(f).map(g).map(h) == s.map(f andThen g andThen h)
    

    I find for-comprehensions to be a bigger win when flatMap and filter are involved. Consider

    for (i <- 1 to 3;
         j <- 1 to 3 if (i + j) % 2 == 0;
         k <- 1 to 3) yield i ^ j ^ k
    

    versus

    (1 to 3).flatMap { i =>
      (1 to 3).filter(j => (i + j) % 2 == 0).flatMap { j =>
        (1 to 3).map { k => i ^ j ^ k }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've read somewhere that functional programming is suitable to take advantage of multi-core trend
First of all,I'm not into web programming. I bumped into django and read a
I have often read that some programming languages are clear than others and I
I read that both multiple returns and multiple goto statements are bad programming practice.
I've read the book Programming Collective Intelligence and found it fascinating. I'd recently heard
I've read the Wikipedia articles for both procedural programming and functional programming , but
I read the following in a review of Knuth's The Art of Computer Programming:
Using C / C++ socket programming, and the read(socket, buffer, BUFSIZE) method. What exactly
What kind of programming problems are state machines most suited for? I have read
Duplicate: What is aspect-oriented programming? Every time I here a podcast or read a

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.