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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:29:54+00:00 2026-05-30T09:29:54+00:00

Suppose there are 3 strings: protein, starch, drink Concatenating those, we could say what

  • 0

Suppose there are 3 strings:

protein, starch, drink

Concatenating those, we could say what is for dinner:

Example:

val protein = "fish"
val starch = "chips"
val drink = "wine"

val dinner = protein + ", " + starch + ", " + drink

But what if something was missing, for example the protein, because my wife couldn’t catch anything. Then, we will have: ,chips, drink for dinner.

There is a slick way to concatenate the strings to optionally add the commas – I just don’t know what it is 😉. Does anyone have a nice idea?

I’m looking for something like:

val dinner = protein +[add a comma if protein is not lenth of zero] + starch .....

It’s just a fun exercise I’m doing, so now sweat if it can’t be done in some cool way. The reason that I’m trying to do the conditional concatenation in a single assignment, is because I’m using this type of thing a lot in XML and a nice solution will make things….. nicer.

  • 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-30T09:29:56+00:00Added an answer on May 30, 2026 at 9:29 am

    When you say “it may be absent”, this entity’s type should be Option[T]. Then,

    def dinner(components: List[Option[String]]) = components.flatten mkString ", "
    

    You would invoke it like this:

    scala> dinner(None :: Some("chips") :: Some("wine") :: Nil)
    res0: String = chips, wine
    

    In case you absolutely want checking a string’s emptiness,

    def dinner(strings: List[String]) = strings filter {_.nonEmpty} mkString ", "
    
    scala> dinner("" :: "chips" :: "wine" :: Nil)
    res1: String = chips, wine
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any ways to do strings concatination in a `build.xml file? Concretely, suppose
Suppose there are two strings: $1 off delicious ham. $1 off delicious $5 ham.
Is there a way to resolve mathematical expressions in strings in javascript? For example,
Imagine there is an arbitrary set of strings. We now suppose that they are
Suppose we have a hash table that maps strings to functions. In my example
suppose there is a string of characters in an array of d[20] . How
There is supposed to be a comma between these two strings foo = ['dumb'
Suppose there is a script sth.py When i logged with 'root' account. type :
Suppose there is a situation like : statement 1; statement 2; statement 3; statement
Suppose there are two lists a = {a1, a2, a3} and b = {b1,

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.