Big Data series Cultivation-scala course 10

Source: Internet
Author: User

Today is mainly about the list in Scala, list in Scala should be very important, next will explain the list of a series of operations

List map, FlatMap, foreach, filter operations explained

1. About the list map, flatmap operation, Difference

2. foreach, filter operation on List

    //a function expression is used in the map in list----but it is an operation on the list, which can represent a function or expressionList (1, 2, 3, 4, 6) Map (_ + 1)//> Res0:list[int] = List (2, 3, 4, 5, 7)Val data = List ("Scala", "Hadoop", "Spark")//> Data:list[string] = List (Scala, Hadoop, Spark)//calculates the length of an element in a listData Map (_.length)//> Res1:list[int] = List (5, 6, 5)//convert LIS element to list and T upside down to make list outputData Map (_.tolist.reverse.mkstring)//> Res2:list[string] = List (Alacs, Poodah, Kraps)//convert elements in list to listData.map (_.tolist)//> Res3:list[list[char]] = list (list (S, C, a, L, a), list (H, A, D, O, O, p), L//Flatmap the list element into a list and then joins the new list output//| ist (S, p, A, r, K))Data.flatmap (_.tolist)//> Res4:list[char] = List (s, C, a, L, a, H, a, D, O, O, p, S, p, A, R, K)//range is left arm right open, [1,10]-->flatmap constituent elementList.range (1, FlatMap) (I-list.range (1, i) map (j =(i, j))) //> res5:list[(int, int)] = List ((2,1), (3,1), (3,2), (4,1), (4,2), (4,3), (5,1 //|), (5,2), (5,3), (5,4), (6,1), (6,2), (6,3), (6,4), (6,5), (7,1), (7,2), (7, //| 3), (7,4), (7,5), (7,6), (8,1), (8,2), (8,3), (8,4), (8,5), (8,6), (8,7), (9 //|, 1), (9,2), (9,3), (9,4), (9,5), (9,6 ), (9,7), (9,8))var sum= 0//> sum:int = 0List (1, 2, 3, 4, 5) foreach (Sum + =_) println ("Sum:" + sum)//> Sum:15//Filter Filtersprintln (List (1, 2, 3, 4, 6, 7, 8, 9, Ten) filter (_% 2 ==0))                                                  //> List (2, 4, 6, 8, ten)println (data filter (_.length = = 5))//> List (Scala, Spark)

List of partition, find, TakeWhile, Dropwhile, span, forall, exsists operations implemented

1. Partition, find, TakeWhile, dropwhile implementations of the list in Scala

2. About span in Scala. ForAll, exsists example explanation

 //partition split into two lists according to an expressionprintln (List (1, 2, 3, 4, 5) partition (_% 2 ==0))                                                  //> (List (2, 4), List (1, 3, 5))//Find finds a list of elements that match an expressionprintln (List (1, 2, 3, 4, 5) Find (_% 2 ==0))//> Some (2)println (List (1, 2, 3, 4, 5) Find (_ <=0))//> None//TakeWhile indicates that the list satisfies the expression form listprintln (List (1, 2, 3, 4, 5) TakeWhile (_ < 4))                                                  //> List (1, 2, 3)//Dropwhile indicates that the list satisfies the expression to form a new list with the oppositeprintln (List (1, 2, 3, 4, 5) Dropwhile (_ < 4))                                                  //> List (4, 5)//span is a combination of the two aboveprintln (List (1, 2, 3, 4, 5) span (_ < 4))//> (List (1, 2, 3), List (4, 5))//exists indicates whether the element that consists of the expression is present--if any, returns true otherwise falsedef hastotallyzerorow (m:list[list[int]) = m exists (row = Row ForAll (_ = = 0))                                                  //> Hastotallyzerorow: (M:list[list[int]]) BooleanVal m= list (list (1,0,0), List (0,1,0), list (0,0,1))                                                  //> M:list[list[int] = list (list (1, 0, 0), list (0, 1, 0), list (0, 0, 1))println (Hastotallyzerorow (m))//> False
Foldleft, Foldright, sortwith operation implementation of list

1.foldLeft, foldright example explanation

  Foldleft source code can be seen in the definition of two functions for summing calculation
def/:[b] (z:b) (OP: (b, A) and b): b = Foldleft (z) (OP) + b): b = foldright (z) (OP) = b) : B = { = z this foreach (x = result = OP (result, x)) result }

2.sortWith Example Explanation

 //  two equals zero-based addition of  println (1 to.). F OldLeft (0) (_+_)) // > 5050  println               ((0/: (1 to 100)) (_+_)) // > 5050  //  println ((1 to 5). Foldright (+) (_-_)) // > -97  println (((1 to 5): \100) (_-_)) // > -97  //  sort list  println (list (1, -3, 4, 2, 6) Sortwith (_ < _)) // > List ( -3, 1, 2, 4, 6)  
Explanation of the operation method of list associated objects

1. Example of apply, make, range for list

2.unzip, flatten, contact, map2 example Explanation

  //list.apply () =list ( )println (List (1, 2, 3))//> List (1, 2, 3)//list.make Copy the same elements//List.make (3, 5)//Range left closed right openprintln (List.range (1, 5))//> List (1, 2, 3, 4)//calculates the element with a spacing of-3println (List.range (9, 1,-3))//> List (9, 6, 3)//ABCDE conversion to list after the combinationVal zipped = "ABCDE". ToList zip List (1, 2, 3, 4, 5)                                                  //> zipped:list[(Char, Int)] = List ((a,1), (b,2), (c,3) , (d,4), (e,5))println (zipped)//> List ((a,1), (b,2), (c,3), (d,4), (e,5))//convert the zipped to a reverseprintln (Zipped.unzip)//> (List (A, B, C, D, E), List (1, 2, 3, 4, 5))//fatter The list into a listprintln (List (' A ', ' B '), List (' C '), List (' d ', ' e ') ). Flatten)//Convert list combination to list//> List (A, B, C, D, E)println (List.concat (list (), list (' B '), List (' C ')))                                                  //> List (b, c)//map2 Two lists are calculated according to the last expression//println (LIST.MAP2 (list), List (Ten)) (_ * _) )

This content is to learn from Liaoliang Teacher's tutorial, his number: 18610086859

Latest Lesson Videos 76: HTTP://YUN.BAIDU.COM/S/1QWKPSPM

The address of the article 35-38 speaks: Http://pan.baidu.com/s/1qWxaAeC

Big Data series Cultivation-scala course 10

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.