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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:10:41+00:00 2026-06-10T11:10:41+00:00

Don’t know if this is possible, but I have some code like this: val

  • 0

Don’t know if this is possible, but I have some code like this:

val list = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
val evens = list.filter { e => e % 2 == 0 }

if(someCondition) {
  val result = evens.filter { e => e % 3 == 0 }
} else {
  val result = evens.filter { e => e % 5 == 0 }
}

But I don’t want to iterate over all elements twice, so is there a way that I can create a “generic pick-all-the-evens numbers on this collection” and apply some other function, so that it would only iterate once?

  • 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-10T11:10:43+00:00Added an answer on June 10, 2026 at 11:10 am

    If you turn list into a lazy collection, such as an Iterator, then you can apply all the filter operations (or other things like map etc) in one pass:

    val list = (1 to 12).toList
    val doubleFiltered: List[Int] =
      list.iterator
        .filter(_ % 2 == 0)
        .filter(_ % 3 == 0)
        .toList
    println(doubleFiltered)
    

    When you convert the collection to an Iterator with .iterator, Scala will keep track of the operations to be performed (here, two filters), but will wait to perform them until the result is actually accessed (here, via the call to .toList).

    So I might rewrite your code like this:

    val list = (1 to 12).toList
    val evens = list.iterator.filter(_ % 2 == 0)
    
    val result = 
      if(someCondition)
        evens.filter(_ % 3 == 0)
      else
        evens.filter(_ % 5 == 0)
    
    result foreach println
    

    Depending on exactly what you want to do, you might want an Iterator, a Stream, or a View. They are all lazily computed (so the one-pass aspect will apply), but they differ on things like whether they can be iterated over multiple times (Stream and View) or whether they keep the computed value around for later access (Stream).

    To really see these different lazy behaviors, try running this bit of code and set <OPERATION> to either toList, iterator, view, or toStream:

    val result =
      (1 to 12).<OPERATION>
        .filter { e => println("filter 1: " + e); e % 2 == 0 }
        .filter { e => println("filter 2: " + e); e % 3 == 0 }
    result foreach println
    result foreach println
    

    Here’s the behavior you will see:

    • List (or any other non-lazy collection): Each filter is requires a separate iteration through the collection. The resulting filtered collection is stored in memory so that each foreach can just display it.
    • Iterator: Both filters and the first foreach are done in a single iteration. The second foreach does nothing since the Iterator has been consumed. Results are not stored in memory.
    • View: Both foreach calls result in their own single-pass iteration over the collection to perform the filters. Results are not stored in memory.
    • Stream: Both filters and the first foreach are done in a single iteration. The resulting filtered collection is stored in memory so that each foreach can just display it.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

don't know better title for this, but here's my code. I have class user
I don't know why, but this code worked for me a month ago... maybe
I don't know if this has been asked before, but what i'd like to
Don't know why but I can't find a solution to this. I have 3
Don't know how to frase this but I found this code wich works as
(Don't know if this is strictly on-topic, but I don't see any better Stack
Don't know if anyone can help me with this or if it's even possible.
Don't they both have to convert to machine code at some point to execute
I don't know if this question is trivial or not. But after a couple
Don't know if this is the right place to ask this, but I will

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.