6 ms·
My favorite Erlang Program (2013)
- lostcolony 10y agoWhat I found interesting (or very, very boring, depending on how you think about it) about this when I first saw it is that this ability is just due to the nature of how receive works. That is, you can receive any kind of message, at any time. Want to listen to a different set of messages? Just call a different receive clause, be that in a different function, or inline. I remember having a coworker who was in love with Akka. He was trying to sell everyone on it, despite my team's having a strong Erlang presence. He showed us Akka's "become", talking about how powerful that could be, and how Erlang didn't have anything comparable. He didn't seem to realize that Akka's needing a special keyword for it was actually a limitation rather than a feature, that Erlang didn't have a special keyword to allow you to do it because it didn't -need- a special keyword to allow you to do it. That's not a dig at Akka, or Scala (it's an impressive language in its own right), but rather to point out how sometimes we miss the complex capabilities a simple set of abstractions can give us.
- qaq 10y agoIs he aware of how Akka came into existence :)?
- _asummers 10y agoRe: Akka, to paraphrase James Gosling's comments on Java "We brought a bunch of Java programmers halfway to Erlang".
- qaq 10y agohalfway is very optimistic interpretation :)
- phyrex 10y agoFYI that wasn't Gosling, that was Guy Steele: http://people.csail.mit.edu/gregs/ll1-discuss-archive-html/msg04045.html http://people.csail.mit.edu/gregs/ll1-discuss-archive-html/m...
- _asummers 10y agoMy mistake! And in retrospect, Guy Steele saying that makes way more sense.
- lmm 10y agoYou don't need to use "become" in Akka - you can just call a function blindly if you want. It's useful to have a distinct concept of "become", because it's useful to not have arbitrary invisible behaviour changes everywhere, but rather demarcate which parts of the system do what. (I mean, just imagine trying to debug a system based on this universal server).
- yetihehe 10y agoNot a problem, in stacktraces you can see where it was called and which line causes errors. Also you can dynamically trace this and even trace only those servers which become what you want. I've debugged systems which behave similarly, it's easy after you learn you can write "debugging servers" which are as simple as this "become server".
- lostcolony 10y agoHow does that work? I've never seen an example, and I don't see how you could do it cleanly without growing the stack, given the JVM's constraints. Also to add onto yetihehe's comment, it's -really freaking easy- to debug such a server as Joe illustrates, or anything else that behaves in such a way (changing behavior by calling another function, to eventually wind up in a different receive statement), in Erlang. It's effectively an FSM determining what kind of messages you handle, the stack traces are clean (TCO means any time you get an exception, it's only in the current 'state'); the only complexities are the fact you're building an FSM, i.e., making sure your transitions are right, and what to do about out of order messages (i.e., a message that doesn't match the current state). There's nothing specific about the Erlang code that makes it harder to debug than the Akka code; in fact, just the fact it's pure Erlang makes it easier; your stack traces are going to be just your own code, no libraries muddying them up. As for demarcation...it's not really any different. That is, Scala's def func:Receive = { //cases } def receive = { case "foo" => become(func) } is equivalent to Erlang's func() -> receive %cases end. receive "foo" -> func() end. But you'll note that in Joe's example he even uses the atom 'become' to indicate the transition is happening, i.e., receive {become, F} => F() end). It's totally equivalent. I'm not saying Scala is less clean, or whathaveyou, just that Akka felt they had to create a new keyword, for some reason, rather than leave it up to the user to implement. Exactly why, or whether it's equivalent to calling a function, I don't know (having never seen an example of the latter, nor commentary on its characteristics re: call stack); my point was simply that it's a natural extension of the language fundamentals in Erlang, but Akka felt the need to create a new keyword just for it, and that people often miss that needed complexity can arise out of a solid set of fundamentals.
- _asummers 10y agoWe've been experimenting with Elixir at work, and it's been really cool to drop down into the Erlang ecosystem and see how people think when they're designing OTP programs, especially when it comes to things like Riak and Mnesia and those sorts of things. My only complaints about this ecosystem is that everything feels scattered in specific places (I've had to read a lot of books, versus being able to read tutorials) and that it's very hard to tell an abandoned project from a feature complete project on GitHub. You can generally look at downloads in the Hex package manager for a rough idea of popularity, but then again build servers can inflate unpopular packages. Someone in the Elixir slack joked to me "75 stars? That's super popular for Erlang!". I can deal with it, but I wish it weren't so. All that said, I think I'm going to stick around with these languages. Everything feels very well designed and battle ready, in stark contrast to using JS. Elixir also seems to remove a lot of the cruft and awkward syntax of Erlang, and having Lisp-style macros makes the GenServer boilerplate go away entirely.
- qaq 10y agoElixir did make Beam/OTP more accessible and more "fun" :)
- _asummers 10y agoAnd not to diminish Jose's work, but a lot of it was a coat of paint -- read the Kernel module, where most of the language core is located, it's pretty much entirely macros. Most of the code in setting up the language, outside of the standard lib, was bootstrapping the macro processor. The better docs, build system, package manager, etc. were all just icing on the cake to make it a 21st century language, with Phoenix as the cherry on top.
- qaq 10y agoI am in no way equating a 20 year effort by Erlang team to that of Elixir team. But I think Elixir is making the whole ecosystem much more accessible. I once tried to bring Erlang into our shop about 6-7 years ago but could not get the team excited about it, with Elixir we will be going into production this Fall and everyone loves it.
- tombert 10y agoWhat I find amusing about this is that raw receives are actually kind of frowned upon in the Erlang community, but the creator is still using them, and the language is still kind of beautiful and cool. This isn't in any way to talk negative about Joe Armstrong or Erlang, but more to show how cool the language is: even non-idiomatic code scales and is wonderful.
- lostcolony 10y agoSlightly tangential, but, I think it's more not using gen* in a supervisor hierarchy is frowned upon. Using raw receives within ~that~ sometimes is the right solution. For instance, I had an instance where I needed to serialize requests out to an external piece of hardware. Various user or system events would determine hardware control events that needed to be sent out, and then responses needed to be listened for to determine if they were successful or not, and return that error the user. Essentially an asynchronous, but serial, process. I had a gen_server to synchronize access to the hardware. Now, the way I implemented this was, with each call that came into the gen_server, I'd send a message via gen_tcp, and then listen for the response as a raw receive inside of the same call. The alternative, to stay in the gen_* structure, would have been to send and finish, and then handle the response in the handle_info (since gen_* is coming back as a raw message). But that would have allowed more sends to occur in between the initial send and getting a response back, which would break the serialization we needed to ensure, and would have lost the reference to who had sent the initial request (since I was reusing the socket, and the acknowledgements from the hardware didn't include any sort of session), making it impossible for me to respond properly to the original caller. So with a raw receive in there, it effectively became like a 'become' server, in that at the top it was a gen_server that implemented a gen_tcp "send" server, and then after sending it temporarily took on the characteristics of a gen_tcp "listen" server via a raw request (though obviously it was listening on the socket even before sending), before reverting back to the "send" server that waited for the next thing to send. In fact, as I recall, while we were serializing our sends, there were certain types of events being broadcast by the hardware that we needed to drop even in the raw receive, so it was recursive. That is, our send server became a listen server, and stayed as a listen server until either we got the response we wanted, or a timeout was hit, at which point we'd revert back to the send server.
- mietek 10y agoHow would this program look like translated to Haskell?
- tome 10y agoid (I'm about two-thirds joking)
- deleted 10y ago[deleted]
- nemoniac 10y ago"It is no exaggeration to regard this as the most fundamental idea in programming: The evaluator, which determines the meaning of expressions in a programming language, is just another program." -- Abelson & Sussman, SICP.