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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:32:06+00:00 2026-05-19T23:32:06+00:00

I’ve got part of code from friend and I’m trying to understand it and

  • 0

I’ve got part of code from friend and I’m trying to understand it and write it in some other way. “gotowe” is a sorted list of ("2011-12-22",-600.00) elements

   val wartosci = gotowe.foldLeft (List(initial_ballance)){
     case ((h::t), x) => (x._2 + h)::h::t
     case _ => Nil
  }.reverse 

That is quite okay but how with this usage of foldLeft? (I’ve put all extra necessary lines):

  val max = wartosci.max
val min = wartosci.min
val wychylenie = if(math.abs(min)>max){math.abs(min)}else{max}

  def scale(x: Double) =
     (x / wychylenie) * 500

  def point(x: Double) =
   {val z:Int =  (500 - x).toInt
z}   

 val (points, _) = wartosci.foldLeft(("", 1)){case ((a, i), h) => (a + " " + (i * 4) + "," + point(scale(h)), i + 1)}

when I print points I’ve got a list of values, and don’t know why not something like pairs of values

  • 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-19T23:32:07+00:00Added an answer on May 19, 2026 at 11:32 pm

    There are a couple of concepts at work here, which we’ll examine in turn to work out what’s going on:

    • foldLeft
    • Pattern matching

    Let’s first look at the definition of foldLeft:

    def foldLeft [B] (z: B)(f: (B, A) ⇒ B) : B

    Applies a binary operator to a start value and all elements of this list, going left to right.

    Returns the result of inserting op between consecutive elements of this list, going left to right with the start value z on the left: op(…op(z, x1), x2, …, xn) where x1,…, xn are the elements of this list.

    So, in your example we’re taking a list of Tuple2[String, Float] (or something like that) and folding it into the value z, which in this case is a List containing one element, initial_balance.

    Now, our f in this case is the code inside the braces. It uses pattern matching to compose a partial function from the pair (b,a) – where in this case b is the ‘cumulative result’ and a is the next item in the list. This is the crux of what a fold does – it collapses the list into a value, using specific rules governing how to add each element at a time.

    What is pattern matching / a partial function? Pattern matching is a very powerful technique for conditioning on and extracting things from input data. We give it something to look for – the case part of the expression – and tell it how to deal with it following the =>. The power of this is that the case expression doesn’t just match, say, numbers or specific strings as might the switch statement in java, but can match, for example, Lists of a certain length, or email addresses, or specific tuples. Even more, you can use it to automatically get certain parts of the match – the domain of the email address, the third element of the list etc.

    We’ll look at the first pattern:

    case ((h::t), x) => (x._2 + h)::h::t

    The left hand side (before the =>) is used to match the value we’re looking for and extract the specific pieces we care about. In this case, we’re looking for a tuple where the first element is a list consisting of a head (h) and a tail(t), and the second element is just the next element of the list. The h::t is an extractor pattern – it’s matching the object ::(h,t) which constructs a List by prepending h onto an existing List t.

    When we’ve matched this, we follow the instructions to the right of the => to fold x into the cumulative value. To do this, we take the right hand side of the date/value tuple (the ._2), add it to the last value in the list (the head), and then push itself on to the head of the list. You’ll notice this is using the same syntax as we used in the pattern match – using :: to prepend elements to a List.

    The effect in this case is to create a running total of what’s going on.

    The second case doesn’t really do much – it’s a catch all case, but as this is being used in a fold it should never get called – we’re always going to return something that looks like ((h::t), x).

    Finally, we reverse the whole thing! So what we’re left with is a list of balances after each transaction, running from oldest to youngest.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am trying to render a haml file in a javascript response like so:

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.