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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:46:07+00:00 2026-05-23T11:46:07+00:00

Consider this BigInt class, which should cache some common values in smallValues : object

  • 0

Consider this BigInt class, which should cache some common values in smallValues:

object BigInt {
  lazy val smallValues = Array(Zero, One, Two)
  lazy val Zero = new BigInt(0, Array[Long]())
  lazy val One = new BigInt(1, Array[Long](1))
  lazy val Two = new BigInt(1, Array[Long](2)) 

  private lazy val cacheSize = smallValues.length


  def apply(num: Long): BigInt = {
    // Is the number cached?
    if (0 <= num && num < cacheSize) smallValues(num.toInt)
    // Figure out the sign and make the number positive after that
    else {
      val (sign, value) = if (num < 0) (-1, num * -1) else (1, num)
      new BigInt(sign, Array(value))
    }
  }
}

class BigInt private(val sign: Int, val num: Array[Long]) extends Ordered[BigInt] {
  println("Constructing BigInt")
  ...
}

The problem here is that accessing one element of the array forces the evaluation of all elements:

scala> BigInt.smallValues(0)
Constructing BigInt
Constructing BigInt
Constructing BigInt
res0: BigInt = BigInt@2c176570

How could I solve that?

Edit: Looking at the proposed solutions I really wonder if it wouldn’t be more efficient to just allocate them without further complication. What do you think?

  • 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-23T11:46:07+00:00Added an answer on May 23, 2026 at 11:46 am

    Editing my answer because I thought this was a toy example of what you want to do and that your real objects were so expensive to build that laziness bought you something. If the question shows something more like real code then laziness makes no sense. The lazy object are bigger and more expensive to create than the strict ones are. Still, I’m keeping the following code because it does show how create a lazy wrapper and that it does “work” (in the sense that it’s functionally correct) even if it doesn’t “work” in the sense of being a good idea for your use case.

    class Lazy[T] (expr : => T) {lazy val ! = expr}
    object Lazy{def apply[T](expr : => T) = new Lazy({expr})}
    
    class BigInt (val sign: Int, val num: Array[Long]) {
      println("Constructing BigInt")
    }
    
    object BigInt {
      val smallValues = Array(
        Lazy(new BigInt(0, Array[Long]())),
        Lazy(new BigInt(1, Array[Long](1))),
        Lazy(new BigInt(1, Array[Long](2)))
      )
    
      private val cacheSize = smallValues.length.toLong
    
       def apply(num: Long): BigInt = {
        // Is the number cached?
        if (0 <= num && num < cacheSize) smallValues(num.toInt)!
        // Figure out the sign and make the number positive after that
        else {
          val (sign, value) = if (num < 0) (-1, num * -1) else (1, num)
          new BigInt(sign, Array(value))
        }
      }
    }
    
    
    scala> BigInt(1)
    Constructing BigInt
    res0: BigInt = BigInt@c0dd841
    
    scala> BigInt(1)
    res1: BigInt = BigInt@c0dd841
    
    scala> BigInt(2)
    Constructing BigInt
    res2: BigInt = BigInt@4a6a00ca
    
    scala> BigInt(2)
    res3: BigInt = BigInt@4a6a00ca
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this problem: I have a program which should fetch (let's say) 100 records
Consider this scenario we have a class A which is singleton it has a
consider this simple scenario: $this->method($arg1, $arg2); Solution: call_user_func_array(array($this,'method'), array($arg1, $arg2)); consider this scenario: $this->object->method($arg1,
Consider this code... using System.Threading; //... Timer someWork = new Timer( delegate(object state) {
Consider this Python program which uses PyGtk and Hippo Canvas to display a clickable
Consider this: public class TestClass { private String a; private String b; public TestClass()
Consider this example (typical in OOP books): I have an Animal class, where each
Consider this function signature: Private Void TextBox1_TextChange(Object Sender, EventArgs e) As far as my
Consider this class: public class TestMap extends HashMap<String, Float> { public static void main(String[]
Consider this code: import java.util.*; class jm45 implements Comparator<jm45> { private int x; jm45(int

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.