Grok all the things

grok (v): to understand (something) intuitively.

Erlang

🙇‍♀️  Students & Apprentices

Greetings, fellow explorers of the tech universe! Today, we're diving into the fascinating world of Erlang, a programming language that has powered some of the most critical systems in telecommunications, web development, and even gaming! Get ready to immerse yourself in its beauty, power, and many quirks.

A Brief History of Erlang 📜

Let's take a step back in time to the late 1980s. At Ericsson, a Swedish multinational company, Joe Armstrong, Robert Virding, and Mike Williams were searching for a solution to improve the development of telecommunication switches. They needed a language that could handle massive concurrency, be highly fault-tolerant, and support hot-swapping of modules. The answer? They created Erlang!

Erlang was released as open source in 1998 and quickly gained popularity for its powerful features. Today, companies like WhatsApp, IBM, and T-Mobile rely on Erlang for their applications. And now, let's unravel its secrets together!

A Glimpse of the Language: Syntax & Semantics 📚

Erlang is a functional programming language based on the Actor Model, which emphasizes computation via message passing between actors. Fear not; we'll break down these concepts step by step.

Functional Programming Fundamentals 🌟

Erlang's functional programming approach means functions are first-class citizens — they can be assigned to variables, passed as arguments, and returned from other functions. Check out this example:

add(X, Y) -> X + Y.
square(X) -> X * X.
compose(F, G) -> fun(X) -> F(G(X)) end.

In the code above, we defined two basic functions: add/2 and square/1. We also defined a higher-order function compose/2, which accepts two functions as arguments and returns their composition. Pretty neat, right?

The Actor Model and Concurrency 🎭

Erlang shines when it comes to concurrency, thanks to the Actor Model. The model includes actors (Erlang processes), which can send and receive messages. Actors can also spawn new actors and modify their internal state.

Take a look at this example:

spawn(fun() -> io:fwrite("Hello, Actor Model!~n") end).

Here, we spawned an Erlang process that executes the anonymous function, which simply writes "Hello, Actor Model!" to the console. In real-world applications, you can imagine how processes might interact to create efficient and scalable systems.

A Touch of OTP (Open Telecom Platform) 📡

OTP is a set of libraries and design principles for building fault-tolerant distributed systems. It might sound intimidating, but once you grasp the key components, the power of OTP becomes evident.

One standout OTP concept is the supervisor. Supervisors oversee worker processes, ensuring they recover from errors and maintain a healthy state. Here's an example of defining a simple supervisor:

-module(my_supervisor).
-behaviour(supervisor).

init(_Args) ->
    {ok, {{one_for_all, 5, 10}, [{worker_1, my_worker, [], permanent, 5000}]}};

In this code snippet, we've defined a basic supervisor named my_supervisor, which oversees a single worker process. If the worker crashes, the supervisor restarts it, ensuring system resilience.

Unleashing Erlang's Power: Telecommunications, Web Development & More 🌐

Erlang's unique features have led to its use in various domains, and we'll explore a few remarkable examples here.

Telecommunications Switches 📞

Erlang's birthplace, telecommunication switches, remain one of its most vital use cases. In this context, Erlang processes model call-handling procedures, messages represent call signaling, and fault tolerance ensures high availability. The language's natural fit for this industry has resulted in its widespread adoption by companies like Ericsson and T-Mobile.

Web Development with the Cowboy Web Server 🤠

Cowboy is an HTTP/1.1, HTTP/2, and Websocket server for Erlang. Developers can build scalable web applications by leveraging Erlang's concurrency and fault tolerance capabilities. Here's an example of a minimal Cowboy web application:

-module(example_handler).
-export([init/2]).
init(_Transport, Req) -> 
    {ok, Req2} = cowboy_req:reply(200, #{<<"content-type">> => <<"text/plain">>}, <<"Hello, Cowboy!">>, Req),
    {ok, Req2, undefined}.

This code defines an HTTP handler that sends a "Hello, Cowboy!" response to incoming requests. Building larger applications with Cowboy typically involves routing and additional middleware.

Online Gaming & Game Servers 🎲

Erlang has become a popular choice for building online game servers due to its ability to manage numerous player connections concurrently. For example, Discord, an online communication platform for gamers, uses Erlang to manage their complex real-time communication infrastructure.

Final Thoughts: Conquering the World with Erlang 💪

As we have seen, Erlang is a powerful language that can handle a wide variety of tasks. From its origins in telecommunications to expanding into web development and gaming, Erlang continues to inspire developers worldwide.

If you're eager to learn more, we recommend checking out Learn You Some Erlang or Erlang/OTP documentation. Happy coding, and may your journey through the world of Erlang be filled with excitement and success!

Grok.foo is a collection of articles on a variety of technology and programming articles assembled by James Padolsey. Enjoy! And please share! And if you feel like you can donate here so I can create more free content for you.