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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:32:34+00:00 2026-05-22T18:32:34+00:00

In Scala, you can do val l = List(1, 2, 3) l.filter(_ > 2)

  • 0

In Scala, you can do

val l = List(1, 2, 3)
l.filter(_ > 2)               // returns a List[Int]
val s = Set("hello", "world")
s.map(_.length)               // returns a Set[Int]

The question is: why is this useful?

Scala collections are probably the only existing collection framework that does this. Scala community seems to agree that this functionality is needed. Yet, noone seems to miss this functionality in the other languages. Example C# (modified naming to match Scala’s):

var l = new List<int> { 1, 2, 3 }
l.filter(i => i > 2)          // always returns Iterable[Int]
l.filter(i => i > 2).toList   // if I want a List, no problem
l.filter(i => i > 2).toSet    // or I want a Set

In .NET, I always get back an Iterable and it is up to me what I want to do with it. (This also makes .NET collections very simple) .

The Scala example with Set forces me to make a Set of lengths out of a Set of string. But what if I just want to iterate over the lengths, or construct a List of lengths, or keep the Iterable to filter it later. Constructing a Set right away seems pointless. (EDIT: collection.view provides the simpler .NET functionality, nice)

I am sure you will show me examples where the .NET approach is absolutely wrong or kills performance, but I just can’t see any (using .NET for years).

  • 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-22T18:32:35+00:00Added an answer on May 22, 2026 at 6:32 pm

    Not a full answer to your question, but Scala never forces you to use one collection type over another. You’re free to write code like this:

    import collection._
    import immutable._
    
    val s = Set("hello", "world")
    val l: Vector[Int] = s.map(_.length)(breakOut)
    

    Read more about breakOut in Daniel Sobral’s detailed answer to another question.

    If you want your map or filter to be evaluated lazily, use this:

    s.view.map(_.length)
    

    This whole behavior makes it easy to integrate your new collection classes and inherit all the powerful capabilities of the standard collection with no code duplication, all of this ensuring that YourSpecialCollection#filter returns an instance of YourSpecialCollection; that YourSpecialCollection#map returns an instance of YourSpecialCollection if it supports the type being mapped to, or a built-in fallback collection if it doesn’t (like what happens of you call map on a BitSet). Surely, a C# iterator has no .toMySpecialCollection method.

    See also: “Integrating new sets and maps” in The Architecture of Scala Collections.

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

Sidebar

Related Questions

scala> val l = List((1,2), (2,3)) l: List[(Int, Int)] = List((1,2), (2,3)) I can
This question was previously asked in scala-user mailing-list without a confirmed answer. scala> val
scala> val alist = List(1,2,3,4,5) alist: List[Int] = List(1, 2, 3, 4, 5) scala>
In scala, I can add to the end of list by, val list =
Can I set maximum size for an actor's mailbox in Scala? Take the Producer-Consumer
Suppose I want to add functionality like map to a Scala List , something
Using Scala 2.7.7: If I have a list of Options, I can flatten them
If I have a list of the form List[Any] like this one val list
Given: case class Foo(a: Int, b: String, c: Double) you can say: val params
I have a list like this: val l= List((Agent, PASS), (Agent, FAIL), (Agent 1,

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.