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

  • Home
  • SEARCH
  • 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 8537767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:58:43+00:00 2026-06-11T10:58:43+00:00

In Scala I have a List with an optional Option . This arises for

  • 0

In Scala I have a List with an optional Option. This arises for example when you use for comprehension on a List and your yield returns an Option. In my case I was processing a JSON object and using for comprehension on the list of fields (List[JField]).

What’s the best way to open up the list and map List() to None and List(Some(a)) to Some(a)?

A first approach would be

def headOrNone[A](list:List[Option[A]]) = 
    list match {
        case Nil => None
        case a::Nil => a
    }

Another approach

def headOrNone[A](list:List[Option[A]]) = list.headOption.getOrElse(None)

A third approach (a variation on the headOption implementation)

def headOrNone[A](list:List[Option[A]]) = if (list.isEmpty) None else list.head

I personally prefer the third approach. Is there a better name for this function than headOrNone and what is the idiomatic scala 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-11T10:58:44+00:00Added an answer on June 11, 2026 at 10:58 am

    How about this:

    def headOrNone[A](list: List[Option[A]]) = list.flatten.headOption
    
    headOrNone(List(Some(4)))   // Some(4)
    headOrNone(List())          // None
    

    Though the first choice has the advantage of giving you an error if you happen to have list with more than one item, which, according to your description, seems like an error condition.

    But personally, I would re-evaluate the code that produces the List[Option[A]] and see if there’s a way to just have it return the right thing in the first place!

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

Sidebar

Related Questions

If I have a List[Option[A]] in Scala, what is the idiomatic way to filter
I have a Scala value of type Option[Set[String]] that I am trying to use
I have a problem: I have a list in scala like this (List(Entry1, Entry2,
I am passing a list to my scala page and I have a form
I have this scala application which is very simple . All that it does
Scala programmer should have known that this sort of writing : class Person{ var
I have a list in scala called l : List[AType] that I want to
Using Scala 2.7.7: If I have a list of Options, I can flatten them
In Scala I have a list of objects that represent points and contain x
Being new to Scala (2.9.1), I have a List[Event] and would like to copy

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.