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

The Archive Base Latest Questions

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

I defined a function to return Fibonacci stream as follows: def fib:Stream[Int] = {

  • 0

I defined a function to return Fibonacci stream as follows:

def fib:Stream[Int] = {
  Stream.cons(1,
    Stream.cons(2,
      (fib zip fib.tail) map {case (x, y) => println("%s + %s".format(x, y)); x + y}))
}

The functions work ok but it looks inefficient (see the output below)

scala> fib take 5 foreach println
1
2
1 + 2
3
1 + 2
2 + 3
5
1 + 2
1 + 2
2 + 3
3 + 5
8

So, it looks like the function calculates the n-th fibonacci number from the very beginning. Is it correct? How would you fix 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-05-27T21:38:28+00:00Added an answer on May 27, 2026 at 9:38 pm

    That is because you have used a def. Try using a val:

    lazy val fib: Stream[Int] 
      = 1 #:: 2 #:: (fib zip fib.tail map { case (x, y) => x + y })
    

    Basically a def is a method; in your example you are calling the method each time and each time the method call constructs a new stream. The distinction between def and val has been covered on SO before, so I won’t go into detail here. If you are from a Java background, it should be pretty clear.

    This is another nice thing about scala; in Java, methods may be recursive but types and values may not be. In scala both values and types can be recursive.

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

Sidebar

Related Questions

Lets assume I have defined function: def threeArgs(x,y,z): return ... # Returns True or
I have this user defined function. public partial class UserDefinedFunctions { static int i;
I'm trying to create a user defined function in Oracle that will return a
What's better practice in a user-defined function in Python: raise an exception or return
I need a user defined function to insert a new record and return the
Can user-defined table-valued (or any other function) return table-type in SQL Server 2008?
I constructed a function defined as var Func1 = function() { return { alert:
I was wondering if a user defined function will always needs a return() statement
I'm trying to define a delegate function that will return an IEnumerable. I'm having
I have a user defined function in SQL called getBuisnessDays it takes @startdate 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.