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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:01:07+00:00 2026-05-14T21:01:07+00:00

In Scala, does calling isEmtpy method on an instance of Stream class cause the

  • 0

In Scala, does calling isEmtpy method on an instance of Stream class cause the stream to be evaluated completely? My code is like this:

import Stream.cons

private val odds: Stream[Int] = cons(3, odds.map(_ + 2))
private val primes: Stream[Int] = cons(2, odds filter isPrime)

private def isPrime(n: Int): Boolean = n match {
  case 1 => false
  case 2 => true
  case 3 => true
  case 5 => true
  case 7 => true
  case x if n % 3 == 0 => false
  case x if n % 5 == 0 => false
  case x if n % 7 == 0 => false
  case x if (x + 1) % 6 == 0 || (x - 1) % 6 == 0 => true
  case x => primeDivisors(x) isEmpty
}


import Math.{sqrt, ceil}
private def primeDivisors(n: Int) =
  primes takeWhile { _ <= ceil(sqrt(n))} filter {n % _ == 0 }

So, does the call to isEmpty on the line case x => primeDivisors(x) isEmpty cause all the prime divisors to be evaluated or only the first one?

  • 1 1 Answer
  • 1 View
  • 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-14T21:01:07+00:00Added an answer on May 14, 2026 at 9:01 pm

    Only if the stream is actually empty 🙂

    Otherwise, it will just see if the stream has a head and tail (matches Stream.cons) and return false.

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

Sidebar

Related Questions

This question is related to calling scala code from java code. When I include
When calling the get method of Scala's HashMap class, which method is used to
Does Scala have a version of Rubys' each_slice from the Array class?
I have this scala application which is very simple . All that it does
Suppose I have the following Scala code: class Foo(a: Int) class Bar(b: Buffer[Int]) extends
I'm wondering why Scala does not have an IO Monad like Haskell. So, in
Just like the question. Does scala promote it same way as Java? Or has
Possible Duplicate: How does Scala's apply() method magic work? I am an absolute beginner
I understand Ruby and Python's yield. What does Scala's yield do?
I've been reading a lot about how Scala and Erlang does lightweight threads and

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.