"Spark Asia-Pacific Research series" Spark Combat Master Road-2nd Chapter hands-on Scala 3rd bar: Hands-on practical Scala Functional Programming (1)

Source: Internet
Author: User

One of the core features of functional programming is to pass functions as parameters to functions, to define functions inside functions, and so on.

  

1, hands-on Scala high-order functions

Declares a list collection instance:

The package that contains the list collection is already pre-defined and automatically imported, so there is no need to import the package here, and this is where the list instantiation object is used, which is actually the Apply method for the list object object;

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

In the above code, 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 we perform the map operation, as follows:

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

In the case of only one parameter, the more concise and normal wording is as follows:

The usual high-order functions are map, filter, reduce, etc., which we will introduce later.

2, hands-on collection in Scala

The collection mainly has list, set, Tuple, map, etc., we follow the hands-on practical way to learn.

We create a list instance in the Eclipse IDE:

Now let's look at the code implementation:

In the source code, it is stated that the internal is the method of apply to complete the instantiation;

in the same way we can instantiate set:

You can also see the implementation of the set instantiation object at this point:

Next we'll look at the set in the command-line terminal, first of all set:

You can see that there are no duplicate elements in the set.

Here's a look at the declaration and use of a tuple:

From the above code can be seen when the source of access to the subscript is starting from 1;

For a tuple, if there are only two elements, it can be created as follows:

Next look at the definition of map:

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

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

Option has two subcategories: some and none, and below we look at the use of option:

Next, take a look at filter processing:

Here's a look at the zip operation for the collection:

Here's a look at the partition of the collection:

We can use flatten's multi-collection for flattening operations:

Flatmap is a combination of map and flatten operations, first map operation and then flatten operation:

"Spark Asia-Pacific Research series" Spark Combat Master Road-2nd Chapter hands-on Scala 3rd bar: Hands-on practical Scala Functional Programming (1)

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.