An example written using seven different functional frameworks and languages

Source: Internet
Author: User
Keywords function example
Tags aliyun coding data data structures design designed for different direction

The goal of this series is to readjust your understanding of functional thinking, to help you think in a new way about http://www.aliyun.com/zixun/aggregation/17253.html "> FAQ, and find ways to improve your daily coding capabilities." This series explores the concept of functional programming, the framework that allows functional programming in the Java language, the functional programming language that runs on the JVM, and the future direction of language design. This series is intended for developers who understand Java and how it works, but have little knowledge of functional languages.

The method of implementing code reuse in functional programming languages is different from object-oriented language, which I have analyzed in the 2nd part. Object-oriented languages tend to have many data structures that can be manipulated, while functional languages have only a handful of data structures that can be manipulated in many ways. Object-oriented languages encourage you to create class-specific methods, and you can capture patterns that recur for later reuse. Functional languages encourage you to apply common transformations to your data structure, and use more advanced functions to customize the actions of specific instances to help you achieve reuse.

The same data structures and operations appear in all functional languages (also appearing in many frameworks that support functional programming in Java), but their names are usually different. A naming style that leads to confusion makes it difficult to convert knowledge of one language into another, even though the underlying concepts are identical.

The goal of this installment is to make this transition a little easier. This article introduces a simple problem that requires policies and iterations, in five languages (Java, Groovy, Clojure, JRuby, and Scala) and two Java functional frameworks (functional Java and totally Lazy) to implement the solution. Each language is implemented in the same way, but the details are very different.

Normal Java

The problem in this article is how to determine whether an integer is a prime number, and the prime number can only be 1 and itself. There are several algorithms for determining prime numbers (some alternative solutions can be found in part 1th); I'm going to use a solution that first determines the factor of the number and then checks that the sum of all the factors equals the number plus 1 to determine whether the number is prime. This is not the most efficient algorithm, but my goal is to show the different implementations of common collection methods, rather than efficiency.

Listing 1 shows the normal Java version:

Listing 1. Common Java Prime number classifier

public class Primenumberclassifier {Private Integer number, public primenumberclassifier (int number) {This.number = Number; public boolean isfactor (int potential) {return number% potential = 0;} public set<integer> getfactors () {set< integer> mx = new hashset<integer> (); Factors.add (1); Factors.add (number); for (Integer i = 2; i < number; i++) if (Isfactor (i)) factors.add (i); return MX; public int sumfactors () {int sum = 0; for (int i:getfactors ()) sum + = i; return sum;} public boolean IsPrime () {return SUMFAC Tors () = = number + 1; }}

If you've read the previous functional thinking series, you'll find that the algorithm in Listing 1 is familiar to the Getfactors () method, which filters the candidate factors.

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.