Java functional programming based on generics (I.)

Source: Internet
Author: User
Programming | Function Note: When you read this article, I hope you can understand and understand the Java Generic (paradigm).


Note: In the FP for Javaparty this weekend, many people seem to have no interest in FP and think that the FP in Java is not very useful. In fact, this is a very large concept of error, paradigm of the development of Java, functional programming support is very large, the impact on the functor is also very large. Functor in algorithms, logic, conditional computing, Rule engine and so on, there will be a great deal, the impact can be much more far-reaching. --To awaken Java developers to a new understanding of FP in Java at the beginning of this article.


Saturday to javaparty the Java functional programming (functional programming in Java), but because of their own preparation is not enough, not to speak deeply. Estimate and write a little blog to add. Of course, the next days, also intend to squeeze some time to delve into the following functor, feel this application will be very wide, even if Java is an OO language, is a state-oriented imperative programming (imperative programming) language.



For the Java language, FP is not a specialty. But functor is a very good new bright spot, although functor this concept is not new, as early as the C + + STL has been proposed: a functor is a function that can be manipulated as a object, or An object representing a single, generic function. Through this description, I think we can say this more: Functor=function Object. So, in OO language, pure function does not exist, in the Java language, function is also an object, this object is functor.

However, in Java, functor but because java5 to fan type (Generic) support, and finally emerged, become a new bright spot. And I also believe that this bright spot will not only occasionally flicker, will be in the future of many applications, show a bright side.



Functor are mainly composed of three types: predicates (predicate), functions (function), procedures (procedure). The predicate evaluates to a Boolean value only; the calculation of the function returns an object; The procedure is calculated only and no results are returned.



predicate
Return a Boolean value

function
Return an Object value

Procedure
Don ' t return anything




Functor itself has one yuan \ Two yuan \ Ternary ... The points. Of course, the most common thing I have is one dollar (unarypredicate,unaryfunction,unaryprocedure) and two yuan (binnarypredicate,binnaryfunction, Binnaryprocedure).



There are some very abstract concepts, so what is a functor?



First, let's take a look at an example, and take a step by step to understand the usefulness of functor and understand what functor is:



public static <T> list<t> Select (collection<t> source, unarypredicate<t> selector) {

list<t> result = new arraylist<t> ();

for (T Item:source) {

if (Selector.test (item)) {

Result.add (item);

}

}

return result;

}




This example is a typical application of functor, using the unary predicate in functor to construct a select algorithm.


(to be continued tomorrow) writed by Hu Great Wall (Silver Fox 999)



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.