Why Erlang? -Partial

Source: Internet
Author: User

Preface

Hope it is not a pitfall. We plan to adopt a structured latency strategy, which will be written before the dead line (July is the planned Erlang month.

 

Perfect plan

The advantages or interesting aspects of Erlang over Java are as follows: imperative language v. S. Functional language, thread abstraction and architecture, and distributed programming support.

 

Cruel and ugly practices

Example 1 fast sorting

JAVA Implementation (data structure and algorithm-sort algorithm-partial http://www.cnblogs.com/zhoujiagen/p/3690243.html)

Erlang implementation

 1 -module(lib_misc). 2  3 %% API 4 -export([for/3, qsort/1]). 5  6 %%%=================================================================== 7 %%% API 8 %%%=================================================================== 9 % simulate ‘for‘ syntax contructor - inner DSL 10 for(Max, Max, F) ->11     [F(Max)];12 for(I, Max, F) ->13     [F(I)|for(I+1, Max, F)].14 15 % quick sort16 qsort([]) ->17     [];18 qsort([Pivot|T]) ->19     qsort([X || X<-T, X < Pivot])20     ++ [Pivot] ++21     qsort([X || X<-T, X > Pivot]).22 23 24 %%--------------------------------------------------------------------25 %% @doc26 %% @spec27 %% @end28 %%--------------------------------------------------------------------29 30 %%%===================================================================31 %%% Internal functions32 %%%===================================================================
View code

The brain hole is wide open, and the language is a programmer's weapon. It is inevitable that the language with the ability to express itself close to the essence of the problem is concise.

 

Example 2 Inheritance

Calculate the area of the image, as long as the area of the rectangle, square, and circle is calculated.

The orthodox oo programmers (including me) need four classes for Java implementation.

Erlang implementation

 1 %%%------------------------------------------------------------------- 2 %%% @author  <[email protected]> 3 %%% @copyright (C) 2014,  4 %%% @doc 5 %%% 6 %%% @end 7 %%% Created :  6 Jul 2014 by  <[email protected]> 8 %%%------------------------------------------------------------------- 9 -module(geometry).10 11 %% API12 -export([area/1]).13 14 %%%===================================================================15 %%% API16 %%%===================================================================17 area({rectangle, Width, Height}) ->18     Width * Height;19 area({square, Side}) ->20     Side * Side;21 area({circle, Radius}) ->22     3.14159 * Radius * Radius.23 24 %%--------------------------------------------------------------------25 %% @doc26 %% @spec27 %% @end28 %%--------------------------------------------------------------------29 30 %%%===================================================================31 %%% Internal functions32 %%%===================================================================
View code

Well, it seems that Java implementation with enumeration can be very concise.

In another example, although I use python, I dare say that the database access code written by python is 80% shorter than the Java JDBC access code.

I just want to express that although a route passes through Rome, most people prefer a short journey.

 

 

 

 

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.