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

The Archive Base Latest Questions

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

In javascript, we can do something like: value = f1() || f2() || f3();

  • 0

In javascript, we can do something like:

value = f1() || f2() || f3();

this will call f1, and assign it to value if the result is not null.
only if the result is null, it will call f2, and assign it to value if that is not null.
…

A way to achieve this in scala is given here: How to make this first-not-null-result function more elegant/concise?
create a getFirstNNWithOption function that calls each function until not null:

value = getFirstNNWithOption(List(f1 _, f2 _, f3 _))

However, this is not as nice as the javascript || operator, which is more flexible. eg:

value = f1() || f2(3) || f3(44, 'ddd') || value4;

is there a way to achieve this in scala?

  • 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-27T21:45:06+00:00Added an answer on May 27, 2026 at 9:45 pm

    I am using the recommended Option instead of null.
    For example:

    class OptionPimp[T](o: Option[T]) {
      def ||(opt: => Option[T]) = o orElse opt
      def ||(t: => T) = o getOrElse t
    }
    implicit def optionPimp[T](o: Option[T]) = new OptionPimp(o)
    
    def f1(): Option[Int] = None
    def f2(): Option[Int] = None
    def f3(): Option[Int] = Some(3)
    
    val value1 = f1() || f2() || f3() || 4 // yields 3
    val value2 = f1() || f2()         || 4 // yields 4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I dynamically name id's using javascript? Something like this: js: var idName
Can I create something like an Internet Explorer accelerator using JavaScript on the client-side?
How can I click in a image/function with javascript? Something like: document.getElementById(check-in-banner).click(); the source
I'm trying to build something on javascript that I can have an input that
I can do an eval(something()); to execute the code dynamically in JavaScript. Is there
I have this scenario, can anybody fill the gaps for me? Its like this:
On my web pages I have source that looks like this: <div id=opt_3 >A)</div>
First of all this is not a question about how can I use http
I'm using JavaScript to set the value of an input with text that may
I've tested this only in Firefox, but apparently you can use an empty string

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.