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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:45:47+00:00 2026-05-25T21:45:47+00:00

Any way to simplify the following? or reduce the boilerplate code with another function?

  • 0

Any way to simplify the following? or reduce the boilerplate code with another function?

scala> val ns = <foo><bar id="1"><tag>one</tag><tag>uno</tag></bar><bar id="2"><tag>two</tag><tag>dos</tag></bar></foo>
ns: scala.xml.Elem = <foo><bar id="1"><tag>one</tag><tag>uno</tag></bar><bar id="2"><tag>two</tag><tag>dos</tag></bar></foo>

scala> (ns \\ "bar" filterNot{_ \\ "@id"  find { _.text == "1" } isEmpty}) \\ "tag"
res0: scala.xml.NodeSeq = NodeSeq(<tag>one</tag>, <tag>uno</tag>)
  • 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-25T21:45:47+00:00Added an answer on May 25, 2026 at 9:45 pm

    I could only find a minor improvement, the find/isEmpty test can be replaced with exists:

    (ns \\ "bar" filter { _ \\ "@id" exists (_.text == "1") }) \\ "tag"
    

    Edit after clarifying comment:

    That’s a really nice idea! Try this for size:

    import xml._
    
    implicit def richNodeSeq(ns: NodeSeq) = new {
    
      def \@(attribMatch: (String, String => Boolean)): NodeSeq =
        ns filter { _ \\ ("@" + attribMatch._1) exists (s => attribMatch._2(s.text)) }
    
    }
    
    ns \\ "bar" \@ ("id", _ == "1") \\ "tag"
    

    I used a predicate instead of hard-coding the attribute value comparison.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to simplify the following code: def get_or_create_user(id, photo=None, first_name=None, last_name=None,
Do I have any way to simplify the following statements? (probably, using boost::enable_if )
Any way I can get this working? I have a block of code: <p><a
Any way to access a nested form_bulder.object? ## controller @project = Project.new @project.tasks.build form_for(@project)
Any way to make a OS X Finder like but valid XHTML/CSS search textfield
Any way to bind an applescript to iTunes so that when iTunes is started
Is any way to tell the solution explorer of Visual Studio 2005 not to
Exists any way to open Folder Options (In windows Explorer: Tools > Folder Options)
Is there any way to check whether a file is locked without using a
Is there any way to capture the MouseDown even from the .NET 2.0 TextBox

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.