Functional Programming (5)-Data structure (functional data structures)

Source: Internet
Author: User

Programming is the process of preparing data for operation. Special operations must use specific data structures to support efficient operations. Without data structure support, we can only declare a memory address for each data, and then use these addresses to manipulate the data, that is, we are familiar with the declaration of variables to read and write this process. Just imagine how many variables we have to declare if there is no data structure. So, the data structure is an indispensable element of any programming.

Functional programming uses a functional data structure (functional data Structure) to support functional functions. Functional data structures are characterized by "immutable characteristics" (immutability), which are necessary for functional combinations (composition) in functional programming. Therefore, unlike other programming categories, the generic structure of functional programming must be specific to a specific set of data operation methods.

The general function according to the structure and the operation method has the following characteristics:

1, non-variable characteristics (immutable)

2. The operation is carried out within the data structure. Try to avoid using intermediate variables

3. The operation returns the new data structure as a result

Let's take a look at the familiar OOP data operation style:

1 scala> var arr = array (2 arr:array[int] = Array (1, 2, 3)
1 scala> var sum = arr (0) +arr (1) +arr (2)2 sum:int = 6

The above operation requires an intermediate variable. And it is done outside the structure: first read the data from the address and then add.

1 scala> arr (0) = sum23 scala> arr4 res9:array[int] = Array (6, 2, 3)

The content of ARR changed after direct assignment. Here arr is the "mutable" (Mutable) data structure. It is certain that we cannot guarantee the content consistency if we need to use arr again.

Then look at the functional style:

1 scala> val arr = Array (2 arr:array[int] = Array (1, 2, 3)3 SCALA&G T Val sum = arr.sum4 sum:int = 6

Functional operations are performed directly within the data structure without the need for intermediate variables.

1 if Else x} 2 Arr1:array[int] = Array (6, 2, 3)34 scala> arr5 res10:array[ INT] = Array (1, 2, 3)

ARR1 is the new data structure after assignment. ARR has not changed. This way we can safely use ARR for the function combination.

There may be some misunderstanding here: ARR1 has copied the data in ARR before modifying the content, so arr has not changed. There's something wrong with this understanding: from the effect, ARR1 is replicating arr. However, in practice, the system simply points to the ARR1 (0) pointer to the node below arr (0) and does not replicate the data in real terms.

Functional Programming (5)-Data structure (functional data structures)

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.