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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:44:06+00:00 2026-06-17T12:44:06+00:00

I have to build a list whose members should be included or not based

  • 0

I have to build a list whose members should be included or not based on a different condition for each.

Let’s say I have to validate a purchase order and, depending on the price, I have to notify a number of people: if the price is more than 10, the supervisor has to be notified. If the price is more than 100 then both the supervisor and the manager. If the price is more than 1000 then the supervisor, the manager, and the director.

My function should take a price as input and output a list of people to notify. I came up with the following:

def whoToNotify(price:Int) = {
  addIf(price>1000, "director",
        addIf(price>100, "manager",
              addIf(price>10, "supervisor", Nil)
        )
       )
}

def addIf[A](condition:Boolean, elem:A, list:List[A]) = {
  if(condition) elem :: list else list
}

Are there better ways to do this in plain Scala? Am I reinventing some wheel here with my addIf function?

Please note that the check on price is just a simplification. In real life, the checks would be more complex, on a number of database fields, and including someone in the organizational hierarchy will not imply including all the people below, so truncate-a-list solutions won’t work.

EDIT

Basically, I want to achieve the following, using immutable lists:

def whoToNotify(po:PurchaseOrder) = {
      val people = scala.collection.mutable.Buffer[String]()
      if(directorCondition(po)) people += "director"
      if(managerCondition(po)) people += "manager"
      if(supervisorCondition(po)) people += "supervisor"
      people.toList
}
  • 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-17T12:44:07+00:00Added an answer on June 17, 2026 at 12:44 pm

    You can use List#flatten() to build a list from subelements. It would even let you add two people at the same time (I’ll do that for the managers in the example below):

    def whoToNotify(price:Int) =
      List(if (price > 1000) List("director") else Nil,
           if (price > 100) List("manager1", "manager2") else Nil,
           if (price > 10) List("supervisor") else Nil).flatten
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to build JSON from two fields. Say, I have a list
I have a list of mp3 files which I automatically build together to different,
I have a situation where I need to dynamically build up a list of
I have a web application that allows users to build a 'client list.' We
I have the following fiddle1 http://jsfiddle.net/y6tCt/35/ . I need to build the checkbox list
I have a list Fragment (importing the v4 support library, build target is 2.3.3
I have a listbox whose values are generated dynamically. The list box contains months
I have build a page that displays a list of ratings in an desc
I have two List<FileInfo> lists, SourceFiles and DestFiles . I want to build a
I have a List and each entry has a dynamic count of attributes which

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.