A simplistic (probably over-simplistic) explanation is they allow processing of the collections. One can consider foldRight and foldLeft to be very similar to a visitor that visits each element and performs a calculation. It does not change the values of the collection, instead it calculates a result from visiting each element. In Java you might see the following pattern:
- class Collection {
- pubic Result op( Result startingValue, Op operation){...}
- }
- interface Op{
- public Result op( Object nextCollectionElem, Result lastCalculatedResult)
- }
To a Java programmer this is pretty obvious how you use it. Reduce operations are similar but very narrow in scope. They only return values that are of the same type as the original collection (IE a list of Int will return an Int from reduce) and there is no initial seed value. The first two inputs are the first and second elements of the collection. If there is only one element then that element is returned.
Reduce might look like the following in Java:
- class Collection
{ - pubic Integer op(Op operation){...}
- }
- interface Op{
- public Integer op( Integer nextElem, Int lastCalculatedResult)
- }
One last point before examples. For fold and reduce operations there are both foldLeft and foldRight and similarly reduceLeft and reduceRight. These indicate the order that a collection is processed. Note that for certain collections one direction is more performant that the other. Lists, for example, are better to process left to right because of the datastructure used.
Some problems that are ideally suited to reduce:
- Calculate the minimum value of a collection
- Calculate the sum of a collection
- scala> val list = List("one", "two", "three")
- list: List[java.lang.String] = List(one, two, three)
- scala> list reduceRight ((elem, result) => {if (elem.length < result.length ) elem else result})
- res2: java.lang.String = two
- // reduce left has the order of parameter reversed.
- scala> list reduceLeft ((result, elem) => {if (elem.length < result.length ) elem else result})
- res4: java.lang.String = one
- // if only 1 element that element is returned
- scala> List("one") reduceRight ((elem, result) => {if (elem.length < result.length ) elem else result})
- res3: java.lang.String = one
- // the two following are equivalent. _ indicates one arguement. If there are two arguments the first _ is the first argument and the second _ is the second argument
- scala> List(1,2,3) reduceRight ((elem, result) => {elem + result})
- res6: Int = 6
- scala> List(1,2,3) reduceRight ( _ + _ )
- res5: Int = 6
- // find minimum. Four equivalent ways to write it
- scala> List(1,2,3) reduceRight ((elem, result) => {elem.min(result)})
- res11: Int = 1
- scala> List(1,2,3) reduceRight ((elem, result) => {elem min result})
- res12: Int = 1
- scala> List(1,2,3) reduceRight (_.min(_))
- res10: Int = 1
- scala> List(1,2,3) reduceRight ( _ min _ )
- res9: Int = 1
No comments:
Post a Comment