• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Playing with reduceLeft, reduceRight, foldLeft, foldRight API in Scala

July 18, 2012 //  by Mohamed Sanaulla//  Leave a Comment

In our previous post we saw in detail about the foreach. map, flatMap and collect methods in the Iterable trait. In this post we will look into detail about reduceLeft, reduceRight, foldLeft, foldRight methods of the Iterable trait. These methods are almost similar in the way the operate on the collection so it should be easier to study them together.

also read:

  • Primary and Auxiliary Constructors in Scala
  • Traits in Scala
  • Java HelloWorld vs Scala HelloWorld

reduceLeft/reduceRight: The scala doc declares this method as:
[code lang=”java”] reduceLeft[B >: A](op: (B, A) ⇒ B): B[/code] which means that this method accepts a function which takes in 2 parameters and returns one value. And the way this method works is that it applies the operation/passed function on 2 elements at a time starting from left, and result of each application is used to compare with the next element until the end of list.
Lets use this reduceLeft to find out the largest and smallest elements in the collection of numbers:
[code lang=”java”] scala> numbers.reduceLeft((x,a) => if ( x < a ) a else x)
res86: Int = 42
//smallest element
scala> numbers.reduceLeft((x,a) => if ( x < a) x else a)
res87: Int = 2
[/code]

reduceRight is exactly similar to reduceLeft the only different being reduceRight applies the operation/passed function on the elements starting from the right of the list.
[code lang=”java”] scala> val numbers = List(4,21,2,4,62,345,67)
numbers: List[Int] = List(4, 21, 2, 4, 62, 345, 67)

//finding maximum
scala> numbers.reduceRight((x,v) => if (x > v) x else v)
res0: Int = 345

//finding minimum
scala> numbers.reduceRight((x,v) => if (x < v) x else v)
res1: Int = 2
[/code]

foldLeft/foldRight: Exactly similar to the way reduceLeft/reduceRight work with a slight difference being foldLeft/foldRight take in an initial value and then apply the initial value to the first element(either leftmost/rightmost) before proceeding like reduceLeft/reduceRight.
Suppose we want to find the sum of the elements in a list (I can use the “sum” method directly, but for understanding I would use foldLeft/reduceLeft):
[code lang=”java”] scala> val numbers = List(1,2,3,4,5,6,7,8,9,10)
numbers: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
//initial seed value of 0 gave the correct sum
scala> numbers.foldLeft(0)((sum,x) => sum + x)
res0: Int = 55

//initial seed value of 10 added 10 to the final sum.
scala> numbers.foldLeft(10)((sum,x) => sum + x)
res1: Int = 65
[/code] One another use of foldLeft is to find the factorial:
[code lang=”java”] //factorial of 5
scala> 1.to(5).foldLeft(1)((prod,x)=>prod*x)
res3: Int = 120

//factorial of 6
scala> 1.to(6).foldLeft(1)((prod,x)=>prod*x)
res4: Int = 720
[/code]

scanLeft/scanRight: It functions exactly similar to foldLeft/foldRight, but instead of returning one value these methods return a collection of values. These collection of values are nothing but the intermediate values obtained while the passed function was applied on the list of numbers.
Lets look at the same factorial example using scanLeft/scanRight:
[code lang=”java”] scala> 1.to(6).scanLeft(1)((prod,x)=>prod*x)
res5: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 1, 2, 6, 24, 120, 720)

scala> 1.to(6).scanRight(1)((prod,x)=>prod*x)
res7: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 6, 30, 120, 360, 720, 720)
[/code]

Category: ScalaTag: scala

About Mohamed Sanaulla

In his day job he works on developing enterprise applications using ADF. He is also the moderator of JavaRanch forums and an avid blogger.

Previous Post: « Creating Hard links and Soft links for a file in Java
Next Post: Listing and filtering directory content using Java NIO2 »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

np.zeros

A Complete Guide To NumPy Functions in Python For Beginners

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact