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

  • Home
  • SEARCH
  • 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 6098825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:12:08+00:00 2026-05-23T13:12:08+00:00

Writing Scala code, I regularly encounter cases where I have processor functions that operate

  • 0

Writing Scala code, I regularly encounter cases where I have “processor” functions that operate iteratively on a collection of elements and also need to know the length of the collection.

On the other hand I have “provider” functions that generate collections and so already know the length. The generated collections may be List[T], Array[T] or Set[T], etc., but even in the case of List[T], my generator knows the size (even if the List type does not store it).

So I would naturally declare the “processor” functions as taking the most generic type that seems to fit all collection types, Iterable[T], as a parameter. However, they then internally need to find out the size via iterative collection traversal at a cost of O(N), which is undesirable.

So my naive solution would be to create a new type like IterableWithSize[T] and have the provider and processor functions create and take this type. Neither Seq[T] nor IndexedSeq[T] seem to fit the bill. But this seems like a relatively common use case, so I’m suspecting that there is a more idiomatic way to do this. What would that be?

  • 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-23T13:12:08+00:00Added an answer on May 23, 2026 at 1:12 pm

    I don’t think there is an idiomatic way to do this. But here are two alternatives:

    (1) Extend Scala’s List/Set/Array collections and override the size method. This is not as difficult as it seems at first glance.

    (2) Wrap your List/Set/Array collections together with the size and define an implicit unwrapper like:

    class IterableWithSizeWrapper[E](private val c: Iterable[E], val size: Int)
    object IterableWithSizeWrapper {
      implicit def unwrap[E](iws: IterableWithSizeWrapper[E]): Iterable[E] = iws.c
    }
    
    object ListWithSizeTest {
    
      def process[E](iws: IterableWithSizeWrapper[E]) {
            // iws.size uses your cached size value
            // iws.take(i) forces the unwrap to the original collect
            // so iws.take(i).size takes the calculated size
        for (i <- 0 to iws.size) assert(iws.take(i).size == i)
      }
    
      def main(args: Array[String]) {
        process(new IterableWithSizeWrapper(List(1,2,3), 3))
        process(new IterableWithSizeWrapper(Set(1,2,3), 3))
        process(new IterableWithSizeWrapper(Array(1,2,3), 3))
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing client code in Scala that needs to interface with a framework
I sense that the Scala community has a little big obsession with writing concise,
So I'm writing some code and have recently come across the need to implement
Writing the code for the user authentication portion of a web site (including account
Writing a JSP page, what exactly does the <c:out> do? I've noticed that the
I writing a report in Visual Studio that takes a user input parameter and
When writing production-quality VC++ code, is the use of recursion acceptable? Why or why
Context: Writing Scala unit-tests in a project managed by SBT. When I execute sbt
I am writing a Scala class to inherit from a Java class, and I
I'm writing a game for Android with Scala. Now I'm trying to get 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.