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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:32:41+00:00 2026-06-10T11:32:41+00:00

I wish to find a match within a List and return values dependant on

  • 0

I wish to find a match within a List and return values dependant on the match. The CollectFirst works well for matching on the elements of the collection but in this case I want to match on the member swEl of the element rather than on the element itself.

abstract class CanvNode (var swElI: Either[CSplit, VistaT])
{         
  private[this] var _swEl: Either[CSplit, VistaT] = swElI
  def member = _swEl
  def member_= (value: Either[CSplit, VistaT] ){ _swEl = value; attach}
  def attach: Unit
  attach

  def findVista(origV: VistaIn): Option[Tuple2[CanvNode,VistaT]] = member match
  {
    case Right(v) if (v == origV) => Option(this, v) 
    case _ => None
  }
}

def nodes(): List[CanvNode] = topNode :: splits.map(i => List(i.n1, i.n2)).flatten

//Is there a better way of implementing this? 
val temp: Option[Tuple2[CanvNode, VistaT]] = 
  nodes.map(i => i.findVista(origV)).collectFirst{case Some (r) => r}

Do I need a View on that, or will the collectFirst method ensure the collection is only created as needed?

It strikes me that this must be a fairly general pattern. Another example could be if one had a List member of the main List’s elements and wanted to return the fourth element if it had one. Is there a standard method I can call? Failing that I can create the following:

implicit class TraversableOnceRichClass[A](n: TraversableOnce[A])
{
  def findSome[T](f: (A) => Option[T]) = n.map(f(_)).collectFirst{case Some (r) => r}
}    

And then I can replace the above with:

val temp: Option[Tuple2[CanvNode, VistaT]] = 
  nodes.findSome(i => i.findVista(origV))

This uses implicit classes from 2.10, for pre 2.10 use:

class TraversableOnceRichClass[A](n: TraversableOnce[A])
{
  def findSome[T](f: (A) => Option[T]) = n.map(f(_)).collectFirst{case Some (r) => r}
}

implicit final def TraversableOnceRichClass[A](n: List[A]):
  TraversableOnceRichClass[A] = new TraversableOnceRichClass(n)
  • 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-10T11:32:43+00:00Added an answer on June 10, 2026 at 11:32 am

    As an introductory side node: The operation you’re describing (return the first Some if one exists, and None otherwise) is the sum of a collection of Options under the “first” monoid instance for Option. So for example, with Scalaz 6:

    scala> Stream(None, None, Some("a"), None, Some("b")).map(_.fst).asMA.sum
    res0: scalaz.FirstOption[java.lang.String] = Some(a)
    

    Alternatively you could put something like this in scope:

    implicit def optionFirstMonoid[A] = new Monoid[Option[A]] {
      val zero = None
      def append(a: Option[A], b: => Option[A]) = a orElse b
    }
    

    And skip the .map(_.fst) part. Unfortunately neither of these approaches is appropriately lazy in Scalaz, so the entire stream will be evaluated (unlike Haskell, where mconcat . map (First . Just) $ [1..] is just fine, for example).


    Edit: As a side note to this side note: apparently Scalaz does provide a sumr that’s appropriately lazy (for streams—none of these approaches will work on a view). So for example you can write this:

    Stream.from(1).map(Some(_).fst).sumr
    

    And not wait forever for your answer, just like in the Haskell version.


    But assuming that we’re sticking with the standard library, instead of this:

    n.map(f(_)).collectFirst{ case Some(r) => r }
    

    I’d write the following, which is more or less equivalent, and arguably more idiomatic:

    n.flatMap(f(_)).headOption
    

    For example, suppose we have a list of integers.

    val xs = List(1, 2, 3, 4, 5)
    

    We can make this lazy and map a function with a side effect over it to show us when its elements are accessed:

    val ys = xs.view.map { i => println(i); i }
    

    Now we can flatMap an Option-returning function over the resulting collection and use headOption to (safely) return the first element, if it exists:

    scala> ys.flatMap(i => if (i > 2) Some(i.toString) else None).headOption
    1
    2
    3
    res0: Option[java.lang.String] = Some(3)
    

    So clearly this stops when we hit a non-empty value, as desired. And yes, you’ll definitely need a view if your original collection is strict, since otherwise headOption (or collectFirst) can’t reach back and stop the flatMap (or map) that precedes it.

    In your case you can skip findVista and get even more concise with something like this:

    val temp = nodes.view.flatMap(
      node => node.right.toOption.filter(_ == origV).map(node -> _)
    ).headOption
    

    Whether you find this clearer or just a mess is a matter of taste, of course.

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

Sidebar

Related Questions

I wish to return from the following powershell function if I find a match
Given an R list, I wish to find the index of a given list
I am attempting to loop through some string arraylists to match elements, but I
I'm have a collection of prefix/value pairs, and wish to find any value in
With my current node selected I wish to find an element <name> [./name] and
I am interating through classes in a Jar file and wish to find those
I have an array of strings(as shown in example). I just wish to find
Given an array of real-valued numbers, A[1..n,1..n] , I wish to find the sub-array
I wish to insert a Date value in DataRow[] and find a DateTime value
Thought my range of search options would easily find this. I wish to combine

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.