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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:23:05+00:00 2026-06-11T23:23:05+00:00

I have often the need to check if many values are equal and in

  • 0

I have often the need to check if many values are equal and in case extract the common value. That is, I need a function that will work like follows:

extract(List()) // None
extract(List(1,2,3)) // None
extract(List(2,2,2)) // Some(2)

Assuming one has a pimp that will add tailOption to seqs (it is trivial to write one or there is one in scalaz), one implementation looks like

def extract[A](l: Seq[A]): Option[A] = {

  def combine(s: A)(r: Seq[A]): Option[A] =
    r.foldLeft(Some(s): Option[A]) { (acc, n) => acc flatMap { v =>
      if (v == n) Some(v) else None
    } }

  for {
    h <- l.headOption
    t <- l.tailOption
    res <- combine(h)(t)
  } yield res
}

Is there something like that – possibly more general – already in Scalaz, or some simpler way to write it?

  • 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-11T23:23:06+00:00Added an answer on June 11, 2026 at 11:23 pm

    This seems like a really complicated way to write

    def extract[A](l:Seq[A]):Option[A] = l.headOption.flatMap(h =>
      if (l.tail.forall(h==)) Some(h) else None)
    

    You don’t need tailOption, since the anonymous function that gets passed as an argument to flatMap is only executed if l is not empty.

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

Sidebar

Related Questions

Often I need to check if some value is blank and write that No
While debugging jQuery apps that use AJAX, I often have the need to see
I often have call hierarchies in that all methods need the same parameters. If
I often have to work with fragile legacy websites that break in unexpected ways
I have a QThread which runs, but will often need to be killed, then
I have an online CVS repository that I need to check code into. However,
I have two scripts that often need to be run with the same parameter:
I often have a situation in my Java code when I need to set
I often have to deal with XML documents that contain namespaced elements, but doesn't
I have a field in my DB that records the last time a check

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.