Thinking in Scala vs Erlang
Caoyuan Blog - - December 16, 2008Keeping 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, {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, b)) :: t) = my_function(x)
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
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
Scala I:
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â?
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"
Part II. Processes vs Actors
Something I
Erlang:
- Lightweight processes
- You can always (almost) 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:
- 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?)
Something II
Erlang:
- Try to service everyone simultaneously
- But may loss service quality when the work is heavy, may time out (out of service)
- Ideal when 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
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 when 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?
Categories: Blogs Caoyuan Blog
Comments
No comments so far, you could be the first.Add comment
Erlang on Twitter
» 9renpoto (Keisuke Kan): EndingがErlangに見えるぐらいに視力落ちてる
» doni_erlang (Dony Erlangga): Otak lo yg lemot bro haha RT @rudyprasetio: Lemot amatt ni…
» dtoader (Dragos Toader): Building Erlang OTP R15B from source in cygwin http://t.co/JhSzPZLY
» erlang_jazz (erlang): Siang menyengat..panas!!@aiaryuki
» erlang_jazz (erlang): Galau dong
» rei_erlang (reiner erlang): RT @PEPATAHKU: Jangan datang pada cinta untuk mencari org yg sempurna, krna itu bisa membuatmu salah dlm menentukan pilihanmu (⌣́_⌣̀)
» ferdymaulana (Fernady Ihmed M): Di hapus RT @GianPratamaP: Alah njalok o erlang RT”@ferdymaulana: Aseeem tobat ge aku wes an ! (cont) http://t.co/KffejaMk
» GianPratamaP (Gian pratama putra): Alah njalok o erlang RT”@ferdymaulana: Aseeem tobat ge aku wes an ! RT @GianPratamaP: Waahh lek japanese guduk basist se seng
» gmcintire (Graham): @tcollen I’m not terribly afraid erlang’s going to suddenly change syntax on me. Though I have a stack of rails < 1.0 book I can’t say same
» RobertLJ (RobertLJ): RT @ErlangSolutions: Mike Williams, one of the inventors of Erlang, is appointed as a member of the Erlang Solutions board. http://t.co/dPv4LWco
Statistics
Number of aggregated posts: 10504
Number of comments: 2145
Most recent article: May 21, 2012
Latest comments
» sunshine on We Who Value Simplicity Have Built Incomprehensible Machines: How to Get Free Credit Scores Having good credit is imperative in today’s world. There are so many things you…
» cheap stickers printings on A Forgotten Principle of Compiler Design: I really like your way of expressing the opinions and sharing the information. It is good to move as chance…
» jamesmathew on This is Why You Spent All that Time Learning to Program: This could be a good achievement for the small area to have a personal news channel and could really help…