Programming in Scala Reading Note 1

Source: Internet
Author: User

Chapter1 and Chapter2

Method definition:

Def methodname (param1: paramtype, param2: paramtype2, [maybe more]): returntype = {

// Method body

}

In functional programming, the conventional saying is,The method returns a value.Otherwise, a method will produce side effects, andAvoid side effectsIt is an original intention of functional programming.

 

Array access:

The array in Scala is array [contenttype]. When declaring an array, you must specify the size of the array and the type of the contained elements:

Val arr = new array [int] (3)

Or

Val arr = array [int] (3)

Or

Val arr = array (1, 2, 3) // directly declare and initialize

Both of the above methods can generate an array object arr, But they produce objects in different ways. New array [int] (3) uses constructors to generate an object, array [int] (3) is an arr array object returned by calling the apply method of array [int.

Something in English: In Scala, We Can instantiate objects, or classes, usingNew. When you instantiate an object in Scala, you canParameterize (determine the parameters)It with values and types.

 

There are several methods with special conventions, but they are just common methods:

Apply (param1: paramtype). If this method exists in a class, the object (TEST) of this class can call test (PARAM). Actually, test is called. apply (PARAM) method.

Update (Param: paramtype) is an object method Update (Param: paramtype), which corresponds to = in test (PARAM) = 12.

1: intlist. This: symbol is read as cons. It is a right operand (right operand), that is, the caller of the method is intlist. The standard is: if a method ends with:, this method is a right operand.

 

Various:

For (ARG <-ARGs) {println (ARG)} // Arg is an unchangeable parameter.

For (I <-0 to 2) {println (ARGs (I)} // I is an immutable Parameter

Args. foreach (ARG => println (ARG) // It is said that this is the most functional programming method.

 

Centralized scaling form:

If a method parameter has only one statement and contains only one parameter, the parameter can be omitted.

Args. foreach (ARG => println (ARG) = args. foreach (println)

If a method has only one parameter and the caller of the method explicitly specifies it, The. And () can be omitted.

1 + 2 = (1). + (2)

 

Tuples

The access to tuples is. _ x, and X starts from 1, one-based.


Mutable objects

Array

Immutable objects

List, tuple,

 

// By default, the set will be immutable set,

VaR testset = set (1, 2, 3)

// Testset is a var, so it can be reasigned. testset + = 4 equals testset = testset + 4

Testset + = 4

Testset + 4 actually returns a new object and assigns it to testset instead of simply appending 4 to the original set.

 

Import scala. collection. mutable. Set

Val testset = set (1, 2, 3)

// Mutable set has the + = method. This method will append the new element to the set and return the original object.

Testset + = 4

That is to say, testset is not re-created, but a new element is added to the original set.

Val indicates that objects cannot be changed, but addresses cannot be changed.

 

To put it more bluntly, Val points to objects that cannot be re-assigned, while var points to objects that can be re-assigned.

Because the set is mutable, there is no need to reassign movieset, which is why it can be a Val.

By contrast, using + = with the immutable set in listing 3.5 required reassigning Jetset, which is why it must be a var.

 

Create a new set or map

If it is null:

Val testset = set [int]()// This () is required

Val testmap = map [int, string] () // same as above

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.