Scala Code — I fail to understand -


i've got part of code friend , i'm trying understand , write in other way. "gotowe" 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 quite okay how usage of foldleft? (i've put 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)), + 1)} 

when print points i've got list of values, , don't know why not pairs of values

there couple of concepts @ work here, we'll examine in turn work out what's going on:

  • foldleft
  • pattern matching

let's first @ definition of foldleft:

def foldleft [b] (z: b)(f: (b, a) ⇒ b) : b

applies binary operator start value , elements of list, going left right.

returns result of inserting op between consecutive elements of list, going left right start value z on left: op(...op(z, x1), x2, ..., xn) x1,..., xn elements of list.

so, in example we're taking list of tuple2[string, float] (or that) , folding value z, in case list containing 1 element, initial_balance.

now, our f in case code inside braces. uses pattern matching compose partial function pair (b,a) - in case b 'cumulative result' , a next item in list. crux of fold - collapses list value, using specific rules governing how add each element @ time.

what pattern matching / partial function? pattern matching powerful technique conditioning on , extracting things input data. give - case part of expression - , tell how deal following =>. power of case expression doesn't match, say, numbers or specific strings might switch statement in java, can match, example, lists of length, or email addresses, or specific tuples. more, can use automatically parts of match - domain of email address, third element of list etc.

we'll @ first pattern:

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

the left hand side (before =>) used match value we're looking , extract specific pieces care about. in case, we're looking tuple first element list consisting of head (h) , tail(t), , second element next element of list. h::t extractor pattern - it's matching object ::(h,t) constructs list prepending h onto existing list t.

when we've matched this, follow instructions right of => fold x cumulative value. this, take right hand side of date/value tuple (the ._2), add last value in list (the head), , push on head of list. you'll notice using same syntax used in pattern match - using :: prepend elements list.

the effect in case create running total of what's going on.

the second case doesn't - it's catch case, being used in fold should never called - we're going return looks ((h::t), x).

finally, reverse whole thing! we're left list of balances after each transaction, running oldest youngest.


Comments

Popular posts from this blog

sql server - python to mssql encoding problem -

java - SNMP4J General Variable Binding Error -

c# - BasicHttpBinding equivalent CustomBinding using WCF Client and PHP WebService -