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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:34:16+00:00 2026-06-04T00:34:16+00:00

I’m new to functional programming, so some problems seems harder to solve using functional

  • 0

I’m new to functional programming, so some problems seems harder to solve using functional approach.

Let’s say I have a list of numbers, like 1 to 10.000, and I want to get the items of the list which sums up to at most a number n (let’s say 100). So, it would get the numbers until their sum is greater than 100.

In imperative programming, it’s trivial to solve this problem, because I can keep a variable in each interaction, and stop once the objective is met.

But how can I do the same in functional programming? Since the sum function operates on completed lists, and I still don’t have the completed list, how can I ‘carry on’ the computation?

If sum was lazily computed, I could write something like that:

           (1 to 10000).sum.takeWhile(_ < 100)

P.S.:Even though any answer will be appreciated, I’d like one that doesn’t compute the sum each time, since obviously the imperative version will be much more optimal regarding speed.

Edit:

I know that I can “convert” the imperative loop approach to a functional recursive function. I’m more interested in finding if one of the existing library functions can provide a way for me not to write one each time I need something.

  • 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-04T00:34:18+00:00Added an answer on June 4, 2026 at 12:34 am

    Use Stream.

    scala> val ss = Stream.from(1).take(10000)
    ss: scala.collection.immutable.Stream[Int] = Stream(1, ?)
    
    scala> ss.scanLeft(0)(_ + _)
    res60: scala.collection.immutable.Stream[Int] = Stream(0, ?)
    
    scala> res60.takeWhile(_ < 100).last
    res61: Int = 91
    

    EDIT:

    Obtaining components is not very tricky either. This is how you can do it:

    scala> ss.scanLeft((0, Vector.empty[Int])) { case ((sum, compo), cur) => (sum + cur, compo :+ cur) }
    res62: scala.collection.immutable.Stream[(Int, scala.collection.immutable.Vector[Int])] = Stream((0,Vector()), ?)
    
    scala> res62.takeWhile(_._1 < 100).last
    res63: (Int, scala.collection.immutable.Vector[Int]) = (91,Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13))
    

    The second part of the tuple is your desired result.

    As should be obvious, in this case, building a vector is wasteful. Instead we can only store the last number from the stream that contributed to sum.

    scala> ss.scanLeft(0)(_ + _).zipWithIndex
    res64: scala.collection.immutable.Stream[(Int, Int)] = Stream((0,0), ?)
    
    scala> res64.takeWhile(_._1 < 100).last._2
    res65: Int = 13
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
I have some data like this: 1 2 3 4 5 9 2 6
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

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.