Erlang and Concurrency
Erlang is famous for its concurrency. Concurrent programming can be used to improve performance, gain scalability and fault-tolerance. BEAM is the virtual machine at the core of the Erlang Open Telecom Platform (OTP) which enables it to happen. By compiling Hamler to CoreErlang, we can essentially take advantage of Erlang VM.
import Prelude
import Data.List as L
start :: IO ()
start = do
pid0 <- getSelf
pids <- seqio [spawn $ loop (State pid0) | x <- [1..1000]]
seqio [send j (Next i) | (i,j) <- (zip pids [last pids|L.init pids]) ]
send (head pids) (Trans "great hamler! " 0)
return ()
data Message = Next Pid
| Trans String Integer
data State = State Pid
handleMsg :: State -> Message -> IO State
handleMsg (State pid) (Next p) = return (State p)
handleMsg (State pid) (Trans str 1111) = return (State pid)
handleMsg (State pid) (Trans str i) =
do send pid (Trans str (i+1))
pid0 <- getSelf
println (show pid0 <> " -> " <> show pid <> ": " <> str <> show i)
return (State pid)
loop :: State -> IO ()
loop s = receive v -> handleMsg s v >>= loop
当前内容版权归 hamler-lang 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 hamler-lang .