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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:12:12+00:00 2026-06-14T06:12:12+00:00

I want to build a List of Foo objects, each constructed by reading off

  • 0

I want to build a List of Foo objects, each constructed by reading off a byte array starting from where the last Foo finished. Given that the only state needed when building are the contents so far and the number of bytes read, I was thinking of doing this by the repeated application of a function transforming a smaller List into a larger one. At each step the current List is passed as a parameter (and maybe the amount of bytes read so far to avoid re-parsing for this) and returned with an element appended. As this seems a pretty straightforward way to build up a List I was interested but unsuccessful in finding library functions that I could use for this. The following is the imperative example of what I am trying to do, I would like to make the functionality in bytesToList stateless and more concise:

def bytesToList(bytes: Array[Byte]): List[Foo] =
{
  var numBytesRead = 0
  var listToBuild = List[Foo]()
  while (numBytesRead < bytes.length)
  {
    listToBuild ::= new Foo(bytes, numBytesRead)
    numBytesRead += listToBuild.last.bytesRead
  }
  listToBuild
}

class Foo(bytesToRead: Array[Byte], startReadingAt: Int)
{val bytesRead = Random.nextInt(bytesToRead.length)}
  • 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-14T06:12:13+00:00Added an answer on June 14, 2026 at 6:12 am

    If you can’t split the array up beforehand, you need to use explicit recursion, something like:

    def bytesToList(bytes: Array[Byte], n: Int = 0): List[Foo] = 
      if (n >= bytes.length) Nil
      else {
        val foo = new Foo(bytes, n) 
        foo :: bytesToList(bytes, n + foo.bytesRead)
      }
    

    I don’t know how may Foos you’re expecting to have, but if it’s more than a thousand or so, it’s a good idea to make this tail recursive to avoid blowing up the stack (hint: add an extra parameter for the output, default value Nil).

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

Sidebar

Related Questions

I want to build a List of images from a website link and then
I have an array of stdClass objects and I want to build a comma
I want to build a list of itens. Each item should be in one
I want to build a list containing every possible permutation of capitalization of a
i am trying to build a statics system i want list all pages in
I want to build a python program that deletes all the photos from my
Say I have a TabControl that is presenting a collection of Foo objects (each
I want to build a list. No problem. for (i = 0; i <
I have a comma-delimited list of directories: foo,bar,baz,qux I want to convert it to
I want to build a list {1, 2, 3, 4, 5} by appending the

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.