Getting started with Scala -09 Scala high-order functions

Source: Internet
Author: User

We do spark development using a lot of spark's higher-order functions so today I'm using scala higher-order functions on Linux Services


Declares a list collection:


The package containing the list is already automatically introduced by the predefined, so there is no need to introduce a package here, directly using the list instantiation object, actually using the list object object's Apply method

We use the map function to multiply each value in the list by 3:


x represents each element in L, and map iterates through each element in L, because there is only one type of element in the list, so we can omit its type when performing a horse-fart operation, as follows:


When there is only one parameter in the list collection, we can remove the parentheses in the argument:


To save all the code, I'm going to use idea.

Collections in Scala:

The collection mainly includes list, set, Tuple, map, etc.

Create a Scala class under the Org.scala.collection under Scala collectionoperations


Create a list instance in idea:


Let's take a look at the list code implementation:


The source code says that its internal is the way of apply to complete the instantiation:

Instantiate the set in the same way:


We can also look at the implementation of the set instantiation object:


Next we cut the set in the Linux command-line terminal, first look at set:


Duplicate elements are not stored in the discovery set

See below for the Declaration and use of the tuple:


When a tuple is accessed, the subscript is starting from 1.

For a tuple, if there are only two elements, we can create them in the following ways:


Now let's look at the definition of map:


The map itself uses a variable parameter, so you can assign multiple values to the map:


Let's take a look at the option type, option represents an optional value:


Option has two sub-classes: Some and none, and look at the use of option:



Next we look at the use of filter:


Zip operation on the collection:



Let's look at the partition of the collection:


Use Flatten's multi-collection for flattening operations:


Flatmap is a collection of map and flatten operations, with the map operation before the flatten operation:



Getting started with Scala -09 Scala high-order functions

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.