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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:56:49+00:00 2026-06-04T02:56:49+00:00

I have a Scala value of type Option[Set[String]] that I am trying to use

  • 0

I have a Scala value of type Option[Set[String]] that I am trying to use in a collection filter method:

val opt: Option[Set[String]] = ...

collection.filter {
  value =>
  opt match {
    case Some(set) => set.contains(value)
    case None => true
  }
}

If the opt value is Some(...) I want to use the enclosed Set to filter the collection, otherwise I want to include all items in the collection.

Is there a better (more idiomatic) way to use the Option (map, filter, getOrElse, etc.)?

The opt comes from an optional command line argument containing a list of terms to include. If the command line argument is missing, then include all terms.

  • 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-04T02:56:50+00:00Added an answer on June 4, 2026 at 2:56 am

    I’d use the fact that Set[A] extends A => Boolean and pass the constant function returning true to getOrElse:

    val p: String => Boolean = opt.getOrElse(_ => true)
    

    Or:

    val p = opt.getOrElse(Function.const(true) _)
    

    Now you can just write collection.filter(p). If you want a one-liner, either of the following will work as well:

    collection filter opt.getOrElse(_ => true)
    collection filter opt.getOrElse(Function.const(true) _)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have something like this in scala: abstract class Point[Type](n: String){ val name =
Say I have a higher kinded type SuperMap[Key[_],Value[_]]`. Suppose now that I had something
I am trying to achieve to write a method that casts a value of
I have a type like this sealed class Foo[A](val value: A) object Foo {
My code is as follows import scala.collection.mutable.HashMap type CrossingInterval = (Date, Date) val crossingMap
I have a Java method that accepts a Long value: (in Java) public void
In trying to use Scala with JPA, I have the following snippet as part
I have coded a view that relies on a scalar function value for one
I have a simple Scala project that runs without any problems inside Eclipse, however,
I have written a Scala (2.9.1-1) application that needs to process several million rows

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.