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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:32:48+00:00 2026-05-29T07:32:48+00:00

Suppose I have an Option[A => Boolean] , a List[A] , and some set

  • 0

Suppose I have an Option[A => Boolean], a List[A], and some set of operations I want to perform on a subset of that list. If the option is set, then I want to filter the list first and then apply my operations. If not, then I want to apply it on the whole list. An example:

val a : Option[Int => Boolean] = Option((a : Int) => a % 2 == 0)
val b = 1 to 100

I can easily do the following:

val c = if (a.isDefined) b.filter(a.get) else b

However, this involves calling a.get; much conditioning leaves me unable to do this! I could alternatively do:

val c = b.filter(a.getOrElse(_ => true))

This feels better, but now I am stuck with a second (albeit trivial) operation being carried out for every element of my sequence. I could hope that it will be optimised out, but this still feels imperfect.

What I would like is something lacking either flaw. It feels like there should be a nice way to do it – any ideas?

  • 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-29T07:32:48+00:00Added an answer on May 29, 2026 at 7:32 am

    You just need to use the normal option handling methods:

    a.map(b.filter).getOrElse(b)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose we have an Option[String], and if there is Some(string) in there, we want
Suppose I have a drop-down list like: <select id='list'> <option value='1'>Option A</option> <option value='2'>Option
Suppose I have linq expression q, then I want to add a sort to
Suppose I have a custom collection class that provides some internal thread synchronization. For
Suppose I have this: <select id=myselect> <option rel=a>1</option> <option rel=b>2</option> <select> How do I
Suppose you have 2 different ASP.NET applications in IIS. Also, you have some ASCX
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
Suppose I have: Toby Tiny Tory Tily Is there an algorithm that can easily
Suppose I have a table called Companies that has a DepartmentID column. There's also
Suppose I have two tables, Customer and Vendor. I want to have a common

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.