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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:00:56+00:00 2026-06-16T23:00:56+00:00

Will any functional language compiler/runtime reduce all chained iterations into one when applyable? From

  • 0

Will any functional language compiler/runtime reduce all chained iterations into one when applyable? From the programmer perspective we could optimize the functional code with such constructs as lazyness and streams but I am interested to know the other side of the story.
My functional example is written in Scala but please don’t limit your answers to that language.

Functional way:

// I assume the following line of code will go
// through the collection 3 times, one for creating it
// one for filtering it and one for summing it 
val sum = (1L to 1000000L).filter(_ % 2 == 0).sum // => 250000500000

I would like the compiler to optimize to the imperative equivalent of:

/* One iteration only */
long sum, i;
for (i = 1L, sum = 0L; i <= 1000000L; i++) {
  if (i % 2 == 0)
    sum += i;
}
  • 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-16T23:00:57+00:00Added an answer on June 16, 2026 at 11:00 pm

    I posted two blog posts about exactly this topic a few years ago:

    http://jnordenberg.blogspot.de/2010/03/scala-stream-fusion-and-specialization.html
    http://jnordenberg.blogspot.de/2010/05/scala-stream-fusion-and-specialization.html

    Note that the specialization and optimization done by the Scala compiler has improved quite a bit since then (probably in Hotspot as well), so the results might be even better today.

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

Sidebar

Related Questions

Dynamic integer will be any number from 0 to 150. i.e. - number returns
Clarification When I mention cross compiling I mean from one language to another (think
I'm considering a functional language that will play well with my environment of C/Objective-C
I want to have a jquery function that will be activated when any checkbox
TL;DR: Adding any non-built-in functions to Array.prototype AND Function.prototype will cause the IE8 native
I just wonder whether there will be any conflict if two service intents are
I'm building an isapi filter that will grab any url requests with the prefix
How can I write a regex which will replace any file name, with it's
I have a mod_rewrite rule that will send any subdomain requests to a php
Can anyone please advise me if there will be any problems changing the wordpress

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.