Reduce, fold or scan (Left/Right)?

前端 未结 3 684
孤城傲影
孤城傲影 2020-11-28 00:35

When should I use reduceLeft, reduceRight, foldLeft, foldRight, scanLeft or scanRight?

3条回答
  •  借酒劲吻你
    2020-11-28 00:55

    Normally REDUCE,FOLD,SCAN method works by accumulating data on LEFT and keep on changing the RIGHT variable. Main difference between them is REDUCE,FOLD is:-

    Fold will always start with a seed value i.e. user defined starting value. Reduce will throw a exception if collection is empty where as fold gives back the seed value. Will always result a single value.

    Scan is used for some processing order of items from left or right hand side, then we can make use of previous result in subsequent calculation. That means we can scan items. Will always result a collection.

    • LEFT_REDUCE method works similar to REDUCE Method.
    • RIGHT_REDUCE is opposite to reduceLeft one i.e. it accumulates values in RIGHT and keep on changing the left variable.

    • reduceLeftOption and reduceRightOption are similar to left_reduce and right_reduce only difference is they return results in OPTION object.

    A part of output for below mentioned code would be :-

    using scan operation over a list of numbers (using seed value 0) List(-2,-1,0,1,2)

    • {0,-2}=>-2 {-2,-1}=>-3 {-3,0}=>-3 {-3,1}=>-2 {-2,2}=>0 scan List(0, -2, -3, -3, -2, 0)

    • {0,-2}=>-2 {-2,-1}=>-3 {-3,0}=>-3 {-3,1}=>-2 {-2,2}=>0 scanLeft (a+b) List(0, -2, -3, -3, -2, 0)

    • {0,-2}=>-2 {-2,-1}=>-3 {-3,0}=>-3 {-3,1}=>-2 {-2,2}=>0 scanLeft (b+a) List(0, -2, -3, -3, -2, 0)

    • {2,0}=>2 {1,2}=>3 {0,3}=>3 {-1,3}=>2 {-2,2}=>0 scanRight (a+b) List(0, 2, 3, 3, 2, 0)

    • {2,0}=>2 {1,2}=>3 {0,3}=>3 {-1,3}=>2 {-2,2}=>0 scanRight (b+a) List(0, 2, 3, 3, 2, 0)

    using reduce,fold operations over a list of Strings List("A","B","C","D","E")

    • {A,B}=>AB {AB,C}=>ABC {ABC,D}=>ABCD {ABCD,E}=>ABCDE reduce (a+b) ABCDE
    • {A,B}=>AB {AB,C}=>ABC {ABC,D}=>ABCD {ABCD,E}=>ABCDE reduceLeft (a+b) ABCDE
    • {A,B}=>BA {BA,C}=>CBA {CBA,D}=>DCBA {DCBA,E}=>EDCBA reduceLeft (b+a) EDCB
    • {D,E}=>DE {C,DE}=>CDE {B,CDE}=>BCDE {A,BCDE}=>ABCDE reduceRight (a+b) ABCDE
    • {D,E}=>ED {C,ED}=>EDC {B,EDC}=>EDCB {A,EDCB}=>EDCBA reduceRight (b+a) EDCBA

    Code :

    object ScanFoldReduce extends App {
    
        val list = List("A","B","C","D","E")
                println("reduce (a+b) "+list.reduce((a,b)=>{
                    print("{"+a+","+b+"}=>"+ (a+b)+"  ")
                    a+b
                }))
    
                println("reduceLeft (a+b) "+list.reduceLeft((a,b)=>{
                    print("{"+a+","+b+"}=>"+ (a+b)+"  ")
                    a+b
                }))
    
                println("reduceLeft (b+a) "+list.reduceLeft((a,b)=>{
                    print("{"+a+","+b+"}=>"+ (b+a)+"  " )
                    b+a
                }))
    
                println("reduceRight (a+b) "+list.reduceRight((a,b)=>{
                    print("{"+a+","+b+"}=>"+ (a+b)+"  " )
                    a+b
                }))
    
                println("reduceRight (b+a) "+list.reduceRight((a,b)=>{
                    print("{"+a+","+b+"}=>"+ (b+a)+"  ")
                    b+a
                }))
    
                println("scan            "+list.scan("[")((a,b)=>{
                    print("{"+a+","+b+"}=>"+ (a+b)+"  " )
                    a+b
                }))
                println("scanLeft (a+b)  "+list.scanLeft("[")((a,b)=>{
                    print("{"+a+","+b+"}=>"+ (a+b)+"  " )
                    a+b
                }))
                println("scanLeft (b+a)  "+list.scanLeft("[")((a,b)=>{
                    print("{"+a+","+b+"}=>"+ (b+a)+"  " )
                    b+a
                }))
                println("scanRight (a+b) "+list.scanRight("[")((a,b)=>{
                    print("{"+a+","+b+"}=>"+ (a+b)+"  " )
                    a+b
                }))
                println("scanRight (b+a) "+list.scanRight("[")((a,b)=>{
                    print("{"+a+","+b+"}=>"+ (b+a)+"  " )
                    b+a
                }))
    //Using numbers
         val list1 = List(-2,-1,0,1,2)
    
                println("reduce (a+b) "+list1.reduce((a,b)=>{
                    print("{"+a+","+b+"}=>"+ (a+b)+"  ")
                    a+b
                }))
    
                println("reduceLeft (a+b) "+list1.reduceLeft((a,b)=>{
                    print("{"+a+","+b+"}=>"+ (a+b)+"  ")
                    a+b
                }))
    
                println("reduceLeft (b+a) "+list1.reduceLeft((a,b)=>{
                    print("{"+a+","+b+"}=>"+ (b+a)+"  " )
                    b+a
                }))
    
                println("      reduceRight (a+b) "+list1.reduceRight((a,b)=>{
                    print("{"+a+","+b+"}=>"+ (a+b)+"  " )
                    a+b
                }))
    
                println("      reduceRight (b+a) "+list1.reduceRight((a,b)=>{
                    print("{"+a+","+b+"}=>"+ (b+a)+"  ")
                    b+a
                }))
    
                println("scan            "+list1.scan(0)((a,b)=>{
                    print("{"+a+","+b+"}=>"+ (a+b)+"  " )
                    a+b
                }))
    
                println("scanLeft (a+b)  "+list1.scanLeft(0)((a,b)=>{
                    print("{"+a+","+b+"}=>"+ (a+b)+"  " )
                    a+b
                }))
    
                println("scanLeft (b+a)  "+list1.scanLeft(0)((a,b)=>{
                    print("{"+a+","+b+"}=>"+ (b+a)+"  " )
                    b+a
                }))
    
                println("scanRight (a+b)         "+list1.scanRight(0)((a,b)=>{
                    print("{"+a+","+b+"}=>"+ (a+b)+"  " )
                    a+b}))
    
                println("scanRight (b+a)         "+list1.scanRight(0)((a,b)=>{
                    print("{"+a+","+b+"}=>"+ (a+b)+"  " )
                    b+a}))
    }
    

提交回复
热议问题