Programming in Scala (Second Edition) Reading notes 15 using list

Source: Internet
Author: User

    1. List is immutable, the underlying implementation uses the data structure on the linked list. Head is the first element, tail is the remaining element

Last is the final element, and Init is an element other than the last element


2. Inserting sorting algorithms

Package Chapter16object Testlist extends app{def isort (X:list[int]): list[int] = if (x.isempty) x else Insert (x.     Head, Isort (x.tail)) def insert (X:int, Xs:list[int]): list[int] = {if (Xs.isempty | | x <= xs.head) x:: XS else Xs.head:: Insert (x, Xs.tail)} val list0 = List (4,5,3,6,1,7,0) println (Isort (list0))}

3. Using the pattern matching to improve the code

Package Chapter16object Testlist extends app{def isort (Xs:list[int]): list[int] = xs Match {case List () = xs Case X::XS1 = insert (x, Isort (XS1))} def insert (X:int, Xs:list[int]): list[int] = xs Match {case List () = = X::xs Case Y::ys = if (x <= y) x:: xs Else Y::insert (x, Ys)} val list0 = List (4,5,3,6,1,7,0) PR Intln (Isort (list0))}

4. First-order method

This section explains most first-order methods defined in theList class. A method is first-order If it does not take any functions as arguments.
A method that does not take a function as a parameter is called a first-order method

5. Stitching to implement reverse method

DEF reverse (Xs:list[int]): list[int] = xs Match {case List () + xs case y::ys = = Reverse (ys)::: List (y)} V Al List0 = List (4,5,3,6,1,7,0) println (reverse (list0))//list (0, 7, 1, 6, 3, 5, 4)

6. Flat List

The flatten method takes a list of lists and flattens it out to a single list

7.drop Remove the top N, take the first n

8.map corresponds to lapply in the R language

9.flatmap if F returns a list, all the return values are stitched together to return


Programming in Scala (Second Edition) Reading notes 15 using list

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.