Scala vs Erlang

Download Report

Transcript Scala vs Erlang

Scala vs Erlang
Caoyuan Deng
What’s Scala
• Creator: Martin Odersky
• http://scala-lang.org
• Features:
– OO + FP
– Compiled to Java bytecode
– Running on JVM, interop with Java libs
– Strong expression ability
– Erlang’s process like actor lib based on message
passing
Syntax
• 1. List comprehension
– Erlang
Lst = [1,2,3,4],
[X + 1 || X <- Lst],
lists:map(fun(X) ->X + 1 end, Lst)
– Scala
val lst =
for (x <lst.map{x
lst.map{_
List(1,2,3,4)
lst) yield x + 1
=> x + 1}
+ 1} // or place holder
Syntax
• 2. Pattern match
– Erlang
case X of
{A, B} when is_integer(A), A > 1 -> ok;
_ -> error
End,
{ok, [{A, B} = H|T]} = my_function(X)
– Scala
x match {
case (a:Int, b:_) if a > 1 => OK// can match type
case _ => ERROR
}
val ("ok", (h@(a, c)) :: t) = my_function(x)
Syntax
• List, Tuple, Array, Map, Binary, Bit
– Erlang
Lst = [1, 2, 3] %% List
[0 | Lst] %% List concat
{1, 2, 3} %% Tuple
<<1, 2, “abc”>>%% Binary
%% no Array, Map syntax
– Scala
val lst = List(1, 2, 3) // List
0 :: lst// List concat
(1, 2, 3) // Tuple
Array(1, 2, 3) // Array
Map(“a” -> 1, “b” -> 2) // Map
// no Binary, Bit syntax
Syntax
• Process, Actor
– Erlang
the_actor(X) ->
receive
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
Syntax
• Process, Actor
– Scala I
class TheActor(x:Int) extends Actor {
def act = loop {
react {
// needs to explicitly exit loop
case “ok” => println(x); exit
case i:Int => x + I
}
}
}
val a = new TheActor(0)
a ! 1
a ! “ok”
Syntax
• Process, Actor
– Scala II
val a = actor {
def loop(x:Int) = {
react {
case “ok” =>println(x)
case i:Int =>loop(x + i)
}
}
loop(0)
}
a ! 1
a ! “ok”
Actors/Processes
• 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
Actors/Process
• Scala
– Active actor is delegated to JVM thread, actor /=
thread
– 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?)
Actor/Process
• Erlang
– Try to service everyone concurrently
– But may loss service quality when the work is heavy,
may time out (out of service)
– Ideal for processing cost is comparable to context
switching cost
– Ideal for small message processing in soft real-time
– Bad for massive data processing, and cpu-heavy work
– Extra bonus: OTP, Distribution
Actor/Process
• 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 processing cost is far more than context
switching cost (context switch time is in ns on modern
JVM)
– When will there be perfect NIO + Actor library?
– Scala OTP (on the way now). Distribution?
IDEs
• ErlyBird, now plugin for NetBeans
– Nov 25
– 549 downloads
• Scalaplugin for NetBeans
– Nov 19
– 1855 download