java1.8 Functional Programming Concepts

Source: Internet
Author: User

About Functional programming

• 1 functions as a class citizen

Features: Passing functions as parameters to another function, function can be used as the return value of another function

• 2 No side effects

The side effect of a function is that the function, in addition to the return value, modifies the state outside the function during the call, for example, the function modifies a global state during the call.

An explicit function means that the only channel that functions exchange data with the outside world is parameters and return values, and explicit functions do not read or modify the external state of the function. In contrast to implicit functions, implicit functions read external information, in addition to parameters and return values, or may modify external information.

• 3 Statement-type (declarative)

For the statement-type programming paradigm, you do not need to provide explicit instructions to operate, all the details of the instructions will be better encapsulated by the library, all you have to do is to make the request you want, stating your intentions.

• 4-tail recursive optimization

The large-scale recursive operation has the potential of stack space overflow error, which also restricts the use of recursive functions and poses some risks to the system.
and the tail recursive optimization can effectively avoid this situation. The tail recursion refers to the recursive operation in the last step of the function. In this case, the function is actually done (the rest of the work is to call itself again), at which point the intermediate result is simply passed to the recursive function of the subsequent call. At this point, the compiler can make an optimization to return the current function call, or overwrite the old function's frame stack with the new function's frame stack. In summary, when recursion is at the last step of a function operation, we can always find ways to avoid recursive operations applying for stack space.

• 5 invariant mode

The so-called invariant is that after the object is created, it no longer changes.

• 6 easy to parallel

Because objects are in the same state, functional programming is easier to parallelize.

• 7 less code

        //use traditional programming to change the state of an object        int[] arr= {1,2,3,4,5};  for(intI=0; i<arr.length;i++) {System. out. Print (arr[i]+=1); //23456} System. out. println ();  for(intI=0; i<arr.length;i++) {System. out. Print (Arr[i]); //23456, the state has been changed the next time the element is acquired        }        //use functional programming without changing the state of an object        int[] arr1= {1,2,3,4,5}; Arrays.stream (ARR1). Map (xx+=1). ForEach (System. out::p rint);//23456Arrays.stream (arr1). ForEach (System. out::p rint);//12345, the next time the element is fetched, the state is still the original value        int[] arr2 = Arrays.stream (arr1). Map (x+=, X1). ToArray ();//arr1 modified data can be taken out, arr1 or the original valueArrays.stream (ARR2). ForEach ((a). System. out. Print (a));//23456        

java1.8 Functional Programming Concepts

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.