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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:38:14+00:00 2026-06-01T17:38:14+00:00

EDIT Ok, @dhg discovered that dot-method syntax required if the code block to fold()

  • 0

EDIT
Ok, @dhg discovered that dot-method syntax required if the code block to fold() is not bound to a val (why with reduce() in the same code block one can use space-method syntax, I don’t know). At any rate, the end result is the nicely concise:

result.map { row =>
  addLink( row.href, row.label )
}.fold(NodeSeq.Empty)(_++_)

Which negates to some degree the original question; i.e. in many cases one can higher-order away either/or scenarios and avoid “fat”, repetitive if/else statements.

ORIGINAL
Trying to reduce if/else handling when working with possibly empty collections like List[T]

For example, let’s say I need to grab the latest news articles to build up a NodeSeq of html news <li><a>links</a></li>:

val result = dao.getHeadlines // List[of model objects]
if(result.isEmpty) NodeSeq.Empty
else 
  result map { row =>
    addLink( row.href, row.label ) // NodeSeq
  } reduce(_ ++ _)

This is OK, pretty terse, but I find myself wanting to go ternary style to address these only-will-ever-be either/or cases:

result.isEmpty ? NodeSeq.Empty :
  result map { row =>
    addLink( row.href, row.label )
  } reduce(_ ++ _)

I’ve seen some old postings on pimping ternary onto boolean, but curious to know what the alternatives are, if any, to streamline if/else?

match {...} is, IMO, a bit bloated for this scenario, and for {...} yield doesn’t seem to help much either.

  • 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-01T17:38:16+00:00Added an answer on June 1, 2026 at 5:38 pm

    You don’t need to check for emptiness at all. Just use fold instead of reduce since fold allows you to specify a default “empty” value:

    scala> List(1,2,3,4).map(_ + 1).fold(0)(_+_)
    res0: Int = 14
    
    scala> List[Int]().map(_ + 1).fold(0)(_+_)
    res1: Int = 0
    

    Here’s an example with a List of Seqs:

    scala> List(1,2).map(Seq(_)).fold(Seq.empty)(_++_)
    res14: Seq[Int] = List(1, 2)
    
    scala> List[Int]().map(Seq(_)).fold(Seq.empty)(_++_)
    res15: Seq[Int] = List()
    

    EDIT: Looks like the problem in your sample has to do with the dropping of dot (.) characters between methods. If you keep them in, it all works:

    scala> List(1,2,3).map(i => node).fold(NodeSeq.Empty)(_ ++ _)
    res57: scala.xml.NodeSeq = NodeSeq(<li><a href="/foo">Link</a></li>, <li><a href="/foo">Link</a></li>, <li><a href="/foo">Link</a></li>)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: Learned that Webmethods actually uses NLST, not LIST, if that matters Our business
Edit: an important piece might be that I'm calling a cfc method via ajax...
EDIT: Sorry about ellipsis that's not what I actually have. For declaring an array
Edit: From another question I provided an answer that has links to a lot
Edit: This was accidentally posted twice. Original: VB.NET Importing Classes I've seen some code
EDIT: Changed as I have a different issue with the same code 2nd Edit:
Edit: I just moved my HTTPRequest code into the onCreate, and now all of
--EDIT-- I believe this is a valid question that may have multiple answers (as
EDIT: When I merge link_def.cpp with link_dec.h, I only get the first error, not
Edit Based on suggestions below to send the logic code GUI delegates, I came

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.