Scala Buzz
Thinking in Scala vs Erlang

0 replies on 1 page.

 Previous Topic Next Topic
 Flat View: This topic has 0 replies on 1 page
Caoyuan Deng

Posts: 165
Nickname: dcaoyuan
Registered: Jan, 2008

 Caoyuan Deng is an independent developer
Thinking in Scala vs Erlang Posted: Dec 15, 2008 11:42 AM

 This post originated from an RSS feed registered with Scala Buzz by Caoyuan Deng. Original Post: Thinking in Scala vs Erlang Feed Title: Blogs about Scala from Caoyuan Feed URL: http://blogtrader.org/page/dcaoyuan/feed/entries/atom?tags=scala Feed Description: Blogs about Scala from Caoyuan Deng Latest Scala Buzz Posts Latest Scala Buzz Posts by Caoyuan Deng Latest Posts From Blogs about Scala from Caoyuan

Keeping Erlang in mind, I've coded two months in Scala, I'm thinking something called "Scala vs Erlang", I wrote some benchmark code to prove me (the code and result may be available someday), and I'd like to do some gradually summary on it in practical aspect. These opinions may be or not be correct currently due to lacking of deep experience and understanding, but, anyway, I need to record them now and correct myself with more experiences and understanding got on both Scala and Erlang.

## Part I. Syntax

### List comprehension

Erlang:

```Lst = [1,2,3,4],
[X + 1 || X <- Lst],
lists:map(fun(X) -> X + 1 end, Lst)
```

Scala:

```val lst = List(1,2,3,4)
for (x <- lst) yield x + 1
lst.map{x => x + 1}
lst.map{_ + 1} // or place holder
```

### Pattern match

Erlang:

```case X of
{A, B} when is_integer(A), A > 1 -> ok
_ -> error
end
```

Scala:

```x match {
case (a:Int, b:_) if a > 1 => OK // can match type
case _ => ERROR
}
```

### List, Tuple, Array, Map, Binary, Bit

Erlang:

```Lst = [1, 2, 3]
[0 | Lst]
{1, 2, 3}
<<1, 2, “abc”>>
%% no Array, Map syntax
```

Scala:

```val lst = List(1, 2, 3)
0 :: lst
(1, 2, 3)
Array(1, 2, 3)
Map(“a” -> 1, “b” -> 2)
// no Binary, Bit syntax
```

### Process, Actor

Erlang:

```the_actor(X) ->
ok -> io:format(“~p~n”, [X])
I -> the_actor(X + I) %% needs to explicitly continue loop
end.
P = spawn(mymodule, the_actor, [0])
P ! 1
P ! ok
```

Scala:

```class TheActor(x:Int) extends Actor {
def act = loop {
react {
case “ok” => println(x); exit // needs to explicitly exit loop
case i:Int => x + i
}
}
}
val a = new TheActor(0)
a ! 1
a ! “ok”
```

## Part II. Processes vs Actors

### Something I

Erlang:

• Lightweight processes
• You can always (almostly) create a new process for each new comer
• Scheduler treats all processes fairly
• Share nothing between processes
• Lightweight context switch between processes
• IO has been carefully delegated to independent processes

Scala:

• You can create a new actor for each new comer
• But the amount of real workers (threads) is dynamically adjusted according to the processing time
• The later comers may be in wait list for further processing until a spare thread is available
• Share nothing or share something upon you decision
• Heavy context switch between working threads
• IO block is still pain unless good NIO framework (Grizzly?)

### Something II

Erlang:

• Try to service each one concurrently
• But may loss service quality when the work is heavy, may time out (out of service)
• Ideal for context switch cost is comparable to processing cost
• Ideal for small message processing in soft real-time
• Bad for massive data processing, and cpu-heavy work

Scala:

• Try to service limited number of customers best first
• If can not service all, the later comers will be put in waiting list and may time out (out of service)
• It’s difficult for soft real-time on all coming concurrent customers
• Ideal for context switch cost is far less than processing cost (context switch time is in ns on modern JVM)
• When will there be perfect NIO + Actor library?

 Previous Topic Next Topic