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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:59:09+00:00 2026-06-11T22:59:09+00:00

I have the following piece of code def my_function(condition: Int=>Boolean): Int = { for

  • 0

I have the following piece of code

def my_function(condition: Int=>Boolean): Int = {
  for (i <- 0 to 10)
    if (condition(i)) return i

  return -1
}

The code is simple: if some condition is met for a number between 1 and 10, return the number, else return invalid result (-1).

It works perfectly fine, but it violates some of the functional programming principles, because of the return in the for cycle. How can I refactor this method (I got unit tests, too) and remove the return statements. I suppose that I must use yield, but it seems to produce list, I just need one value.

  • 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-11T22:59:10+00:00Added an answer on June 11, 2026 at 10:59 pm

    This is the functional translation of your code:

    def my_function(condition: Int => Boolean): Int = {
      (0 to 10).find(i => condition(i)).getOrElse(-1)
    }
    

    Or more succinctly as:

    def my_function(condition: Int => Boolean): Int = {
      (0 to 10).find(condition).getOrElse(-1)
    }
    

    I’ve used the find method which takes, as an argument, a function that produces a Boolean. The find method returns the first item in the collection that satisfies the condition. It returns the item as an Option so that if there is no satisfying item, then the result will be None. The getOrElse method of Option will return the found result if there is one, and return -1 if there isn’t.

    However, you should not use “error codes” like returning -1 in Scala programming. They are bad practice. Instead your should throw an exception or return an Option[Int] such that returning None indicates no value was found. The right way to do this would be something like:

    def my_function(condition: Int => Boolean): Option[Int] = {
      (0 to 10).find(condition)
    }
    
    println(my_function(_ > 5))   // Some(6)
    println(my_function(_ > 11))  // None
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following piece of code def show unless logged_in? login_required return end
If I have the following piece of Ruby code: class Blah def self.bleh @blih
I have the following piece of code that takes in some words, stores them
I have the following piece of code in ItemController.groovy def list = { params.max
I have following piece of code: class A { public C GetC() { return
I have following piece of code: class Test{ private: int id; public: Test(int v):id(v)
I have following piece of code: customObject* object; std::list<customObject> objects; for(int i = 0;
I have the following piece of code: def f = new File(test.txt) f.write(test, UTF-8)
I have following simple piece of code, which is a part of ipv6 handling
I have the following piece of latex code: \def\a{1} \def\b{2} \def\c{\a+\b} \def\d{\c/2} I expected

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.