Zum Hauptinhalt springen

Long post to test postprocessing

Elixir (programming language)

Appearance

Text

  • Small

    Standard

    Large

Width

  • Standard

    Wide

Color (beta)

  • Automatic

    Light

    Dark

From Wikipedia, the free encyclopedia

(Öffnet in neuem Fenster)

This article relies excessively on references (Öffnet in neuem Fenster) to primary sources (Öffnet in neuem Fenster). Please improve this article by adding secondary or tertiary sources (Öffnet in neuem Fenster).
Find sources: "Elixir" programming language (Öffnet in neuem Fenster) – news (Öffnet in neuem Fenster) · newspapers (Öffnet in neuem Fenster) · books (Öffnet in neuem Fenster) · scholar (Öffnet in neuem Fenster) · JSTOR (Öffnet in neuem Fenster) (June 2023) (Learn how and when to remove this message (Öffnet in neuem Fenster))

Elixir

elixir programming language (Öffnet in neuem Fenster)

Elixir

Paradigms (Öffnet in neuem Fenster)multi-paradigm (Öffnet in neuem Fenster): functional (Öffnet in neuem Fenster), concurrent (Öffnet in neuem Fenster), distributed (Öffnet in neuem Fenster), process-oriented (Öffnet in neuem Fenster)Designed by (Öffnet in neuem Fenster)José ValimFirst appeared2012; 13 years agoStable release (Öffnet in neuem Fenster)

1.18.4[1] (Öffnet in neuem Fenster) 

Edit this on Wikidata (Öffnet in neuem Fenster)

/ 21 May 2025; 4 days ago

Typing disciplinedynamic (Öffnet in neuem Fenster), strong (Öffnet in neuem Fenster)Platform (Öffnet in neuem Fenster)Erlang (Öffnet in neuem Fenster)License (Öffnet in neuem Fenster)Apache License 2.0 (Öffnet in neuem Fenster)[2] (Öffnet in neuem Fenster)Filename extensions (Öffnet in neuem Fenster).ex, .exsWebsiteelixir-lang.org (Öffnet in neuem Fenster)Influenced byClojure (Öffnet in neuem Fenster), Erlang (Öffnet in neuem Fenster), Ruby (Öffnet in neuem Fenster)InfluencedGleam (Öffnet in neuem Fenster), LFE (Öffnet in neuem Fenster)

Elixir is a functional (Öffnet in neuem Fenster), concurrent (Öffnet in neuem Fenster), high-level (Öffnet in neuem Fenster) general-purpose (Öffnet in neuem Fenster) programming language (Öffnet in neuem Fenster) that runs on the BEAM (Öffnet in neuem Fenster) virtual machine (Öffnet in neuem Fenster), which is also used to implement the Erlang (Öffnet in neuem Fenster) programming language.[3] (Öffnet in neuem Fenster) Elixir builds on top of Erlang and shares the same abstractions for building distributed (Öffnet in neuem Fenster), fault-tolerant (Öffnet in neuem Fenster) applications. Elixir also provides tooling and an extensible (Öffnet in neuem Fenster) design. The latter is supported by compile-time metaprogramming (Öffnet in neuem Fenster) with macros (Öffnet in neuem Fenster) and polymorphism (Öffnet in neuem Fenster) via protocols.[4] (Öffnet in neuem Fenster)

The community organizes yearly events in the United States,[5] (Öffnet in neuem Fenster) Europe,[6] (Öffnet in neuem Fenster) and Japan,[7] (Öffnet in neuem Fenster) as well as minor local events and conferences.[8] (Öffnet in neuem Fenster)[9] (Öffnet in neuem Fenster)

History

José Valim created the Elixir programming language as a research and development (Öffnet in neuem Fenster) project at Plataformatec. His goals were to enable higher extensibility and productivity in the Erlang VM while maintaining compatibility with Erlang's ecosystem.[10] (Öffnet in neuem Fenster)[11] (Öffnet in neuem Fenster)

Elixir is aimed at large-scale sites and apps. It uses features of Ruby (Öffnet in neuem Fenster), Erlang, and Clojure (Öffnet in neuem Fenster) to develop a high-concurrency and low-latency language. It was designed to handle large data volumes. Elixir is also used in telecommunications, e-commerce, and finance.[12] (Öffnet in neuem Fenster)

In 2021, the Numerical Elixir effort was announced with the goal of bringing machine learning, neural networks, GPU compilation, data processing, and computational notebooks to the Elixir ecosystem.[13] (Öffnet in neuem Fenster)

Versioning

Each of the minor versions supports a specific range of Erlang/OTP (Öffnet in neuem Fenster) versions.[14] (Öffnet in neuem Fenster) The current stable release version is 1.18.4[1] (Öffnet in neuem Fenster) .

Features

Examples

The following examples can be run in an iex shell (Öffnet in neuem Fenster) or saved in a file and run from the command line (Öffnet in neuem Fenster) by typing elixir <filename>.

Classic Hello world (Öffnet in neuem Fenster) example:

iex> IO.puts("Hello World!")
Hello World!

Pipe operator:

iex> "Elixir" |> String.graphemes() |> Enum.frequencies()
%{"E" => 1, "i" => 2, "l" => 1, "r" => 1, "x" => 1}

iex> %{values: 1..5} |> Map.get(:values) |> Enum.map(& &1 * 2)
[2, 4, 6, 8, 10]

iex> %{values: 1..5} |> Map.get(:values) |> Enum.map(& &1 * 2) |> Enum.sum()
30

Pattern matching (Öffnet in neuem Fenster) (a.k.a. destructuring):

iex> %{left: x} = %{left: 5, right: 8}
iex> x
5

iex> {:ok, [_ | rest]} = {:ok, [1, 2, 3]}
iex> rest
[2, 3]

Pattern matching with multiple clauses:

iex> case File.read("path/to/file") do
iex>   {:ok, contents} -> IO.puts("found file: #{contents}")
iex>   {:error, reason} -> IO.puts("missing file: #{reason}")
iex> end

List comprehension (Öffnet in neuem Fenster):

iex> for n <- 1..5, rem(n, 2) == 1, do: n*n
[1, 9, 25]

Asynchronously reading files with streams:

1..5
|> Task.async_stream(&File.read!("#{&1}.txt"))
|> Stream.filter(fn {:ok, contents} -> String.trim(contents) != "" end)
|> Enum.join("\n")

Multiple function bodies with guards (Öffnet in neuem Fenster):

def fib(n) when n in [0, 1], do: n
def fib(n), do: fib(n-2) + fib(n-1)

Relational databases with the Ecto library:

schema "weather" do
  field :city     # Defaults to type :string
  field :temp_lo, :integer
  field :temp_hi, :integer
  field :prcp,    :float, default: 0.0
end

Weather |> where(city: "Kraków") |> order_by(:temp_lo) |> limit(10) |> Repo.all

Sequentially spawning a thousand processes:

for num <- 1..1000, do: spawn fn -> IO.puts("#{num * 2}") end

Asynchronously (Öffnet in neuem Fenster) performing a task:

task = Task.async fn -> perform_complex_action() end
other_time_consuming_action()
Task.await task

[citation needed (Öffnet in neuem Fenster)]

See also

References

  1. Wlaschin, Scott (May 2013). "Railway Oriented Programming" (Öffnet in neuem Fenster). F# for Fun and Profit. Archived (Öffnet in neuem Fenster) from the original on 30 January 2021. Retrieved 28 February 2021.

Further reading

Programming languages (Öffnet in neuem Fenster)

Authority control databases (Öffnet in neuem Fenster): National

Edit this at Wikidata (Öffnet in neuem Fenster)

Categories (Öffnet in neuem Fenster):

Elm (programming language)

Tools

Appearance

Text

  • Small

    Standard

    Large

Width

  • Standard

    Wide

Color (beta)

  • Automatic

    Light

    Dark

From Wikipedia, the free encyclopedia

(Öffnet in neuem Fenster)

This article relies excessively on references (Öffnet in neuem Fenster) to primary sources (Öffnet in neuem Fenster). Please improve this article by adding secondary or tertiary sources (Öffnet in neuem Fenster).
Find sources: "Elm" programming language (Öffnet in neuem Fenster) – news (Öffnet in neuem Fenster) · newspapers (Öffnet in neuem Fenster) · books (Öffnet in neuem Fenster) · scholar (Öffnet in neuem Fenster) · JSTOR (Öffnet in neuem Fenster) (May 2019) (Learn how and when to remove this message (Öffnet in neuem Fenster))

Elm

(Öffnet in neuem Fenster)

The Elm tangram

Paradigm (Öffnet in neuem Fenster)functional (Öffnet in neuem Fenster)FamilyHaskell (Öffnet in neuem Fenster)Designed by (Öffnet in neuem Fenster)Evan CzaplickiFirst appearedMarch 30, 2012; 13 years ago[1] (Öffnet in neuem Fenster)Stable release (Öffnet in neuem Fenster)

0.19.1 / October 21, 2019; 5 years ago[2] (Öffnet in neuem Fenster)

Typing discipline (Öffnet in neuem Fenster)static (Öffnet in neuem Fenster), strong (Öffnet in neuem Fenster), inferred (Öffnet in neuem Fenster)Platform (Öffnet in neuem Fenster)x86-64 (Öffnet in neuem Fenster)OS (Öffnet in neuem Fenster)macOS (Öffnet in neuem Fenster), Windows (Öffnet in neuem Fenster)License (Öffnet in neuem Fenster)Permissive (Öffnet in neuem Fenster) (Revised BSD (Öffnet in neuem Fenster))[3] (Öffnet in neuem Fenster)Filename extensions (Öffnet in neuem Fenster).elmWebsiteelm-lang.org (Öffnet in neuem Fenster)

Edit this at Wikidata (Öffnet in neuem Fenster)

Influenced byHaskell (Öffnet in neuem Fenster), Standard ML (Öffnet in neuem Fenster), OCaml (Öffnet in neuem Fenster), F# (Öffnet in neuem Fenster)InfluencedRedux (Öffnet in neuem Fenster),[4] (Öffnet in neuem Fenster) Rust (Öffnet in neuem Fenster),[5] (Öffnet in neuem Fenster) Vue (Öffnet in neuem Fenster),[6] (Öffnet in neuem Fenster) Roc,[7] (Öffnet in neuem Fenster) Derw,[8] (Öffnet in neuem Fenster) Gren[9] (Öffnet in neuem Fenster)

Elm is a domain-specific (Öffnet in neuem Fenster) programming language (Öffnet in neuem Fenster) for declaratively (Öffnet in neuem Fenster) creating web browser (Öffnet in neuem Fenster)-based graphical user interfaces (Öffnet in neuem Fenster). Elm is purely functional (Öffnet in neuem Fenster), and is developed with emphasis on usability (Öffnet in neuem Fenster), performance, and robustness (Öffnet in neuem Fenster). It advertises "no runtime (Öffnet in neuem Fenster) exceptions (Öffnet in neuem Fenster) in practice",[10] (Öffnet in neuem Fenster) made possible by the Elm compiler's static type checking (Öffnet in neuem Fenster).

History

Elm was initially designed by Evan Czaplicki as his thesis in 2012.[11] (Öffnet in neuem Fenster) The first release of Elm came with many examples and an online editor that made it easy to try out in a web browser (Öffnet in neuem Fenster).[12] (Öffnet in neuem Fenster) Czaplicki joined Prezi (Öffnet in neuem Fenster) in 2013 to work on Elm,[13] (Öffnet in neuem Fenster) and in 2016 moved to NoRedInk (Öffnet in neuem Fenster) as an Open Source Engineer, also starting the Elm Software Foundation.[14] (Öffnet in neuem Fenster)

The initial implementation of the Elm compiler targets HyperText Markup Language (HTML (Öffnet in neuem Fenster)), Cascading Style Sheets (Öffnet in neuem Fenster) (CSS), and JavaScript (Öffnet in neuem Fenster).[15] (Öffnet in neuem Fenster) The set of core tools has continued to expand, now including a read–eval–print loop (Öffnet in neuem Fenster) (REPL),[16] (Öffnet in neuem Fenster) package manager (Öffnet in neuem Fenster),[17] (Öffnet in neuem Fenster) time-travelling debugger,[18] (Öffnet in neuem Fenster) and installers for macOS (Öffnet in neuem Fenster) and Windows (Öffnet in neuem Fenster).[19] (Öffnet in neuem Fenster) Elm also has an ecosystem of community created libraries (Öffnet in neuem Fenster),[20] (Öffnet in neuem Fenster) and Ellie, an advanced online editor that allows saved work and including community libraries.[21] (Öffnet in neuem Fenster)

Features

Elm has a small set of language constructs, including traditional if-expressions, let-expressions for storing local values, and case-expressions for pattern matching (Öffnet in neuem Fenster).[22] (Öffnet in neuem Fenster) As a functional language, it supports anonymous functions (Öffnet in neuem Fenster), functions as arguments, and functions can return functions, the latter often by partial application of curried (Öffnet in neuem Fenster) functions. Functions are called by value. Its semantics include immutable values, stateless functions (Öffnet in neuem Fenster), and static typing with type inference. Elm programs render HTML through a virtual DOM, and may interoperate with other code by using "JavaScript as a service".

Immutability

All values in Elm are immutable (Öffnet in neuem Fenster), meaning that a value cannot be modified after it is created. Elm uses persistent data structures (Öffnet in neuem Fenster) to implement its arrays, sets, and dictionaries in the standard library.[23] (Öffnet in neuem Fenster)

Static types

Elm is statically typed. Type annotations are optional (due to type inference) but strongly encouraged. Annotations exist on the line above the definition (unlike C-family languages where types and names are interspersed). Elm uses a single colon to mean "has type".

Types include primitives like integers and strings, and basic data structures such as lists, tuples, and records. Functions have types written with arrows, for example round : Float -> Int. Custom types (Öffnet in neuem Fenster) allow the programmer to create custom types to represent data in a way that matches the problem domain.[24] (Öffnet in neuem Fenster)

Types can refer to other types, for example a List Int. Types are always capitalized; lowercase names are type variables. For example, a List a is a list of values of unknown type. It is the type of the empty list and of the argument to List.length, which is agnostic to the list's elements. There are a few special types that programmers create to interact with the Elm runtime. For example, Html Msg represents a (virtual) DOM tree whose event handlers all produce messages of type Msg.

Rather than allow any value to be implicitly nullable (such as JavaScript's undefined or a null pointer (Öffnet in neuem Fenster)), Elm's standard library defines a Maybe a type. Code that produces or handles an optional value does so explicitly using this type, and all other code is guaranteed a value of the claimed type is actually present.

Elm provides a limited number of built-in type classes (Öffnet in neuem Fenster): number which includes Int and Float to facilitate the use of numeric operators such as (+) or (*), comparable which includes numbers, characters, strings, lists of comparable things, and tuples of comparable things to facilitate the use of comparison operators, and appendable which includes strings and lists to facilitate concatenation with (++). Elm does not provide a mechanism to include custom types into these type classes or create new type classes (see Limits (Öffnet in neuem Fenster)).

Module system

Elm has a module system (Öffnet in neuem Fenster) that allows users to break their code into smaller parts called modules. Modules can hide implementation details such as helper functions, and group related code together. Modules serve as a namespace for imported code, such as Bitwise.and. Third party libraries (or packages) consist of one or more modules, and are available from the Elm Public Library (Öffnet in neuem Fenster). All libraries are versioned according to semver (Öffnet in neuem Fenster), which is enforced by the compiler and other tools. That is, removing a function or changing its type can only be done in a major release.

Interoperability with HTML, CSS, and JavaScript

Elm uses an abstraction called ports to communicate with JavaScript (Öffnet in neuem Fenster).[25] (Öffnet in neuem Fenster) It allows values to flow in and out of Elm programs, making it possible to communicate between Elm and JavaScript.

Elm has a library called elm/html that a programmer can use to write HTML and CSS within Elm.[26] (Öffnet in neuem Fenster) It uses a virtual DOM (Öffnet in neuem Fenster) approach to make updates efficient.[27] (Öffnet in neuem Fenster)

Backend

Elm does not officially support server-side development. Czaplicki does consider it a primary goal at this point, but public progress on this front has been slow. Nevertheless, there are several independent projects which attempt to explore Elm on the backend.

The primary production-ready full-stack Elm platform is Lamdera, an open-core "unfork" of Elm.[28] (Öffnet in neuem Fenster)[29] (Öffnet in neuem Fenster)[30] (Öffnet in neuem Fenster) Czaplicki has also teased Elm Studio, a potential alternative to Lamdera, but it isn't available to the public yet.[31] (Öffnet in neuem Fenster) Current speculation is that Elm Studio will use a future version of Elm that targets C, uses Emscripten to compile to WASM, and supports type-safe Postgres (Öffnet in neuem Fenster) table generation.[32] (Öffnet in neuem Fenster)[33] (Öffnet in neuem Fenster)

For full-stack frameworks, as opposed to BaaS (Öffnet in neuem Fenster) products, elm-pages is perhaps the most popular fully open-source option.[34] (Öffnet in neuem Fenster) It does not extend the Elm language, but just runs the compiled JS on Node.js (Öffnet in neuem Fenster). It also supports scripting. There is also Pine, an Elm to .NET compiler, which allows safe interop with C#, F#, and other CLR (Öffnet in neuem Fenster) languages.[35] (Öffnet in neuem Fenster)

There were also some attempts in Elm versions prior to 0.19.0 to use the BEAM (Erlang virtual machine) (Öffnet in neuem Fenster) to run Elm, but they are stuck due to the removal of native code in 0.19.0 and changes to the package manager. One of the projects executed Elm directly on the environment,[36] (Öffnet in neuem Fenster) while another one compiled it to Elixir.[37] (Öffnet in neuem Fenster)

Finally, the Gren programming language started out a fork of Elm primarily focused on backend support, although its goals have since shifted.

The Elm Architecture (TEA pattern)

The Elm Architecture is a software design pattern (Öffnet in neuem Fenster) and as a TLA (Öffnet in neuem Fenster) called TEA pattern for building interactive web applications. Elm applications are naturally constructed in that way, but other projects may find the concept useful.

An Elm program is always split into three parts:

  • Model - the state of the application

  • View - a function that turns the model into HTML

  • Update - a function that updates the model based on messages

Those are the core of the Elm Architecture.

For example, imagine an application that displays a number and a button that increments the number when pressed.[38] (Öffnet in neuem Fenster) In this case, all we need to store is one number, so our model can be as simple as type alias Model = Int. The view function would be defined with the Html library and display the number and button. For the number to be updated, we need to be able to send a message to the update function, which is done through a custom type such as type Msg = Increase. The Increase value is attached to the button defined in the view function such that when the button is clicked by a user, Increase is passed on to the update function, which can update the model by increasing the number.

In the Elm Architecture, sending messages to update is the only way to change the state. In more sophisticated applications, messages may come from various sources: user interaction, initialization of the model, internal calls from update, subscriptions to external events (window resize, system clock, JavaScript interop...) and URL changes and requests.

Limits

Elm does not support higher-kinded polymorphism (Öffnet in neuem Fenster),[39] (Öffnet in neuem Fenster) which related languages Haskell (Öffnet in neuem Fenster), Scala (Öffnet in neuem Fenster) and PureScript (Öffnet in neuem Fenster) offer, nor does Elm support the creation of type classes (Öffnet in neuem Fenster).

This means that, for example, Elm does not have a generic map function which works across multiple data structures such as List and Set. In Elm, such functions are typically invoked qualified by their module name, for example calling List.map and Set.map. In Haskell or PureScript, there would be only one function map. This is a known feature request that is on Czaplicki's rough roadmap since at least 2015.[40] (Öffnet in neuem Fenster) On the other hand, implementations of TEA pattern in advanced languages like Scala (Öffnet in neuem Fenster) does not suffer from such limitations and can benefit from Scala (Öffnet in neuem Fenster)'s type classes, type-level (Öffnet in neuem Fenster) and kind-level (Öffnet in neuem Fenster) programming constructs.[41] (Öffnet in neuem Fenster)

Another outcome is a large amount of boilerplate code (Öffnet in neuem Fenster) in medium to large size projects as illustrated by the author of "Elm in Action," a former Elm core team member, in his single page application example[42] (Öffnet in neuem Fenster) with almost identical fragments being repeated in update, view, subscriptions, route parsing and building functions.

Example code

-- This is a single line comment.

{-
This is a multi-line comment.
It is {- nestable. -}
-}

-- Here we define a value named `greeting`. The type is inferred as a `String`.
greeting =
    "Hello World!"

-- It is best to add type annotations to top-level declarations.
hello : String
hello =
    "Hi there."

-- Functions are declared the same way, with arguments following the function name.
add x y =
    x + y

-- Again, it is best to add type annotations.
hypotenuse : Float -> Float -> Float
hypotenuse a b =
    sqrt (a^2 + b^2)

-- We can create lambda functions with the `\[arg] -> [expression]` syntax.
hello : String -> String
hello = \s -> "Hi, " ++ s

-- Function declarations may have the anonymous parameter names denoted by `_`, 
-- which are matched but not used in the body. 
const : a -> b -> a
const k _ = k

-- Functions are also curried; here we've curried the multiplication 
-- infix operator with a `2`
multiplyBy2 : number -> number
multiplyBy2 =
    (*) 2

-- If-expressions are used to branch on `Bool` values
absoluteValue : number -> number
absoluteValue number =
    if number < 0 then negate number else number

-- Records are used to hold values with named fields
book : { title : String, author : String, pages : Int }
book =
    { title = "Steppenwolf"
    , author = "Hesse"
    , pages = 237 
    }

-- Record access is done with `.`
title : String
title =
    book.title

-- Record access `.` can also be used as a function
author : String
author =
    .author book

-- We can create tagged unions with the `type` keyword.
-- The following value represents a binary tree.
type Tree a
    = Empty
    | Node a (Tree a) (Tree a)

-- It is possible to inspect these types with case-expressions.
depth : Tree a -> Int
depth tree =
    case tree of
        Empty -> 0
        Node _ left right ->
            1 + max (depth left) (depth right)

See also

References

  1. "Main.elm" (Öffnet in neuem Fenster). github.com/rtfeldman/elm-spa-example. Retrieved 30 June 2020.

External links

Haskell (Öffnet in neuem Fenster) programming

Programming languages (Öffnet in neuem Fenster)

Categories (Öffnet in neuem Fenster):

Gleam (programming language)

Tools

Text

  • Small

    Standard

    Large

Width

  • Standard

    Wide

Color (beta)

  • Automatic

    Light

    Dark

From Wikipedia, the free encyclopedia

The topic of this article may not meet Wikipedia's general notability guideline (Öffnet in neuem Fenster). Please help to demonstrate the notability of the topic by citing reliable secondary sources (Öffnet in neuem Fenster) that are independent (Öffnet in neuem Fenster) of the topic and provide significant coverage of it beyond a mere trivial mention. If notability cannot be shown, the article is likely to be merged (Öffnet in neuem Fenster), redirected (Öffnet in neuem Fenster), or deleted (Öffnet in neuem Fenster).
Find sources: "Gleam" programming language (Öffnet in neuem Fenster) – news (Öffnet in neuem Fenster) · newspapers (Öffnet in neuem Fenster) · books (Öffnet in neuem Fenster) · scholar (Öffnet in neuem Fenster) · JSTOR (Öffnet in neuem Fenster) (March 2024) (Learn how and when to remove this message (Öffnet in neuem Fenster))

Gleam

(Öffnet in neuem Fenster)

Lucy, the starfish mascot for Gleam[1] (Öffnet in neuem Fenster)

Paradigm (Öffnet in neuem Fenster)Multi-paradigm (Öffnet in neuem Fenster): functional (Öffnet in neuem Fenster), concurrent (Öffnet in neuem Fenster)[2] (Öffnet in neuem Fenster)Designed by (Öffnet in neuem Fenster)Louis PilfoldDeveloper (Öffnet in neuem Fenster)Louis PilfoldFirst appearedJune 13, 2016; 8 years agoStable release (Öffnet in neuem Fenster)

1.10.0[3] (Öffnet in neuem Fenster) 

Edit this on Wikidata (Öffnet in neuem Fenster)

/ 14 April 2025

Typing discipline (Öffnet in neuem Fenster)Type-safe (Öffnet in neuem Fenster), static (Öffnet in neuem Fenster), inferred (Öffnet in neuem Fenster)[2] (Öffnet in neuem Fenster)Memory management (Öffnet in neuem Fenster)Garbage collected (Öffnet in neuem Fenster)Implementation languageRust (Öffnet in neuem Fenster)OS (Öffnet in neuem Fenster)FreeBSD (Öffnet in neuem Fenster), Linux (Öffnet in neuem Fenster), macOS (Öffnet in neuem Fenster), OpenBSD (Öffnet in neuem Fenster), Windows (Öffnet in neuem Fenster)[4] (Öffnet in neuem Fenster)License (Öffnet in neuem Fenster)Apache License 2.0 (Öffnet in neuem Fenster)[5] (Öffnet in neuem Fenster)Filename extensions (Öffnet in neuem Fenster).gleamWebsitegleam.run (Öffnet in neuem Fenster)Influenced by

[6] (Öffnet in neuem Fenster)

Gleam is a general-purpose (Öffnet in neuem Fenster), concurrent (Öffnet in neuem Fenster), functional (Öffnet in neuem Fenster) high-level (Öffnet in neuem Fenster) programming language (Öffnet in neuem Fenster) that compiles to Erlang (Öffnet in neuem Fenster) or JavaScript (Öffnet in neuem Fenster) source code.[2] (Öffnet in neuem Fenster)[7] (Öffnet in neuem Fenster)[8] (Öffnet in neuem Fenster)

Gleam is a statically-typed language,[9] (Öffnet in neuem Fenster) which is different from the most popular languages that run on Erlang’s virtual machine BEAM (Öffnet in neuem Fenster), Erlang (Öffnet in neuem Fenster) and Elixir (Öffnet in neuem Fenster). Gleam has its own type-safe implementation of OTP, Erlang's actor framework.[10] (Öffnet in neuem Fenster) Packages are provided using the Hex package manager, and an index for finding packages written for Gleam is available.[11] (Öffnet in neuem Fenster)

History

The first numbered version of Gleam was released on April 15, 2019.[12] (Öffnet in neuem Fenster) Compiling to JavaScript was introduced with version v0.16.[13] (Öffnet in neuem Fenster)

In 2023 the Erlang Ecosystem Foundation funded the creation of a course for learning Gleam on the learning platform Exercism (Öffnet in neuem Fenster).[14] (Öffnet in neuem Fenster)

Version v1.0.0 was released on March 4, 2024.[15] (Öffnet in neuem Fenster)

Features

Gleam includes the following features, many common to other functional programming languages:[8] (Öffnet in neuem Fenster)

Example

A "Hello, World!" (Öffnet in neuem Fenster) example:

import gleam/io

pub fn main() {
  io.println("hello, world!")
}

Gleam supports tail call (Öffnet in neuem Fenster) optimization:[16] (Öffnet in neuem Fenster)

pub fn factorial(x: Int) -> Int {
  // The public function calls the private tail recursive function
  factorial_loop(x, 1)
}

fn factorial_loop(x: Int, accumulator: Int) -> Int {
  case x {
    1 -> accumulator

    // The last thing this function does is call itself
    _ -> factorial_loop(x - 1, accumulator * x)
  }
}

Implementation

Gleam's toolchain is implemented in the Rust programming language (Öffnet in neuem Fenster).[17] (Öffnet in neuem Fenster) The toolchain is a single native binary executable which contains the compiler, build tool, package manager, source code formatter, and language server (Öffnet in neuem Fenster). A WebAssembly (Öffnet in neuem Fenster) binary containing the Gleam compiler is also available, enabling Gleam code to be compiled within a web browser.

References

  1. "gleam-lang/gleam" (Öffnet in neuem Fenster). Gleam. May 6, 2024. Retrieved May 6, 2024.

External links

Stub icon (Öffnet in neuem Fenster)

This programming-language (Öffnet in neuem Fenster)-related article is a stub (Öffnet in neuem Fenster). You can help Wikipedia by expanding it (Öffnet in neuem Fenster).

Categories (Öffnet in neuem Fenster):

Erlang (programming language)

Tools

Text

  • Small

    Standard

    Large

Width

  • Standard

    Wide

Color (beta)

  • Automatic

    Light

    Dark

From Wikipedia, the free encyclopedia

Erlang

(Öffnet in neuem Fenster)

Paradigms (Öffnet in neuem Fenster)Multi-paradigm (Öffnet in neuem Fenster): concurrent (Öffnet in neuem Fenster), functional (Öffnet in neuem Fenster)Designed by (Öffnet in neuem Fenster)

Developer (Öffnet in neuem Fenster)Ericsson (Öffnet in neuem Fenster)First appeared1986; 39 years agoStable release (Öffnet in neuem Fenster)

27.3.4[1] (Öffnet in neuem Fenster) 

Edit this on Wikidata (Öffnet in neuem Fenster)

/ 8 May 2025; 9 days ago

Typing disciplineDynamic (Öffnet in neuem Fenster), strong (Öffnet in neuem Fenster)License (Öffnet in neuem Fenster)Apache License 2.0 (Öffnet in neuem Fenster)Filename extensions (Öffnet in neuem Fenster).erl, .hrlWebsitewww.erlang.org (Öffnet in neuem Fenster)Major implementations (Öffnet in neuem Fenster)ErlangInfluenced byLisp (Öffnet in neuem Fenster), PLEX (Öffnet in neuem Fenster),[2] (Öffnet in neuem Fenster) Prolog (Öffnet in neuem Fenster), Smalltalk (Öffnet in neuem Fenster)InfluencedAkka (Öffnet in neuem Fenster), Clojure (Öffnet in neuem Fenster),[3] (Öffnet in neuem Fenster) Dart (Öffnet in neuem Fenster), Elixir (Öffnet in neuem Fenster), F# (Öffnet in neuem Fenster), Opa (Öffnet in neuem Fenster), Oz (Öffnet in neuem Fenster), Reia (Öffnet in neuem Fenster), Rust (Öffnet in neuem Fenster),[4] (Öffnet in neuem Fenster) Scala (Öffnet in neuem Fenster), Go (Öffnet in neuem Fenster)

Erlang (/ˈɜːrlæŋ/ (Öffnet in neuem Fenster) UR-lang (Öffnet in neuem Fenster)) is a general-purpose (Öffnet in neuem Fenster), concurrent (Öffnet in neuem Fenster), functional (Öffnet in neuem Fenster) high-level (Öffnet in neuem Fenster) programming language (Öffnet in neuem Fenster), and a garbage-collected (Öffnet in neuem Fenster) runtime system (Öffnet in neuem Fenster). The term Erlang is used interchangeably with Erlang/OTP, or Open Telecom Platform (Öffnet in neuem Fenster) (OTP), which consists of the Erlang runtime system (Öffnet in neuem Fenster), several ready-to-use components (OTP) mainly written in Erlang, and a set of design principles (Öffnet in neuem Fenster) for Erlang programs.[5] (Öffnet in neuem Fenster)

The Erlang runtime system (Öffnet in neuem Fenster) is designed for systems with these traits:

The Erlang programming language (Öffnet in neuem Fenster) has immutable (Öffnet in neuem Fenster) data, pattern matching (Öffnet in neuem Fenster), and functional programming (Öffnet in neuem Fenster).[7] (Öffnet in neuem Fenster) The sequential subset of the Erlang language supports eager evaluation (Öffnet in neuem Fenster), single assignment (Öffnet in neuem Fenster), and dynamic typing (Öffnet in neuem Fenster).

A normal Erlang application is built out of hundreds of small Erlang processes.

It was originally proprietary software (Öffnet in neuem Fenster) within Ericsson (Öffnet in neuem Fenster), developed by Joe Armstrong (Öffnet in neuem Fenster), Robert Virding, and Mike Williams in 1986,[8] (Öffnet in neuem Fenster) but was released as free and open-source software (Öffnet in neuem Fenster) in 1998.[9] (Öffnet in neuem Fenster)[10] (Öffnet in neuem Fenster) Erlang/OTP is supported and maintained by the Open Telecom Platform (OTP) product unit at Ericsson (Öffnet in neuem Fenster).

History

The name Erlang, attributed to Bjarne Däcker, has been presumed by those working on the telephony switches (for whom the language was designed) to be a reference to Danish mathematician and engineer Agner Krarup Erlang (Öffnet in neuem Fenster) and a syllabic abbreviation (Öffnet in neuem Fenster) of "Ericsson Language".[8] (Öffnet in neuem Fenster)[11] (Öffnet in neuem Fenster)[12] (Öffnet in neuem Fenster) Erlang was designed with the aim of improving the development of telephony applications.[13] (Öffnet in neuem Fenster) The initial version of Erlang was implemented in Prolog (Öffnet in neuem Fenster) and was influenced by the programming language PLEX (Öffnet in neuem Fenster) used in earlier Ericsson exchanges. By 1988 Erlang had proven that it was suitable for prototyping telephone exchanges, but the Prolog interpreter was far too slow. One group within Ericsson estimated that it would need to be 40 times faster to be suitable for production use. In 1992, work began on the BEAM (Öffnet in neuem Fenster) virtual machine (VM), which compiles Erlang to C using a mix of natively compiled code and threaded code (Öffnet in neuem Fenster) to strike a balance between performance and disk space.[14] (Öffnet in neuem Fenster) According to co-inventor Joe Armstrong, the language went from laboratory product to real applications following the collapse of the next-generation AXE telephone exchange (Öffnet in neuem Fenster) named AXE-N (Öffnet in neuem Fenster) in 1995. As a result, Erlang was chosen for the next Asynchronous Transfer Mode (Öffnet in neuem Fenster) (ATM) exchange AXD.[8] (Öffnet in neuem Fenster)

(Öffnet in neuem Fenster)
Robert Virding and Joe Armstrong, 2013

In February 1998, Ericsson Radio Systems banned the in-house use of Erlang for new products, citing a preference for non-proprietary languages.[15] (Öffnet in neuem Fenster) The ban caused Armstrong and others to make plans to leave Ericsson.[16] (Öffnet in neuem Fenster) In March 1998 Ericsson announced the AXD301 switch,[8] (Öffnet in neuem Fenster) containing over a million lines of Erlang and reported to achieve a high availability (Öffnet in neuem Fenster) of nine "9"s (Öffnet in neuem Fenster).[17] (Öffnet in neuem Fenster) In December 1998, the implementation of Erlang was open-sourced and most of the Erlang team resigned to form a new company, Bluetail AB.[8] (Öffnet in neuem Fenster) Ericsson eventually relaxed the ban and re-hired Armstrong in 2004.[16] (Öffnet in neuem Fenster)

In 2006, native symmetric multiprocessing (Öffnet in neuem Fenster) support was added to the runtime system and VM.[8] (Öffnet in neuem Fenster)

Processes

Erlang applications are built of very lightweight Erlang processes in the Erlang runtime system. Erlang processes can be seen as "living" objects (object-oriented programming (Öffnet in neuem Fenster)), with data encapsulation and message passing (Öffnet in neuem Fenster), but capable of changing behavior during runtime. The Erlang runtime system provides strict process isolation (Öffnet in neuem Fenster) between Erlang processes (this includes data and garbage collection, separated individually by each Erlang process) and transparent communication between processes (see Location transparency (Öffnet in neuem Fenster)) on different Erlang nodes (on different hosts).

Joe Armstrong, co-inventor of Erlang, summarized the principles of processes in his PhD (Öffnet in neuem Fenster) thesis (Öffnet in neuem Fenster):[18] (Öffnet in neuem Fenster)

  • Everything is a process.

  • Processes are strongly isolated.

  • Process creation and destruction is a lightweight operation.

  • Message passing is the only way for processes to interact.

  • Processes have unique names.

  • If you know the name of a process you can send it a message.

  • Processes share no resources.

  • Error handling is non-local.

  • Processes do what they are supposed to do or fail.

Joe Armstrong remarked in an interview with Rackspace in 2013: "If Java (Öffnet in neuem Fenster) is 'write once, run anywhere (Öffnet in neuem Fenster)', then Erlang is 'write once, run forever'."[19] (Öffnet in neuem Fenster)

Usage

In 2014, Ericsson (Öffnet in neuem Fenster) reported Erlang was being used in its support nodes, and in GPRS (Öffnet in neuem Fenster), 3G (Öffnet in neuem Fenster) and LTE (Öffnet in neuem Fenster) mobile networks worldwide and also by Nortel (Öffnet in neuem Fenster) and Deutsche Telekom (Öffnet in neuem Fenster).[20] (Öffnet in neuem Fenster)

Erlang is used in RabbitMQ (Öffnet in neuem Fenster). As Tim Bray (Öffnet in neuem Fenster), director of Web Technologies at Sun Microsystems (Öffnet in neuem Fenster), expressed in his keynote at O'Reilly Open Source Convention (Öffnet in neuem Fenster) (OSCON) in July 2008:

If somebody came to me and wanted to pay me a lot of money to build a large scale message handling system that really had to be up all the time, could never afford to go down for years at a time, I would unhesitatingly choose Erlang to build it in.

Erlang is the programming language used to code WhatsApp (Öffnet in neuem Fenster).[21] (Öffnet in neuem Fenster)

It is also the language of choice for Ejabberd (Öffnet in neuem Fenster) – an XMPP (Öffnet in neuem Fenster) messaging server.

Elixir (Öffnet in neuem Fenster) is a programming language that compiles into BEAM byte code (via Erlang Abstract Format).[22] (Öffnet in neuem Fenster)

Since being released as open source, Erlang has been spreading beyond telecoms, establishing itself in other vertical markets such as FinTech, gaming, healthcare, automotive, Internet of Things and blockchain. Apart from WhatsApp, there are other companies listed as Erlang's success stories, including Vocalink (Öffnet in neuem Fenster) (a MasterCard company), Goldman Sachs (Öffnet in neuem Fenster), Nintendo (Öffnet in neuem Fenster), AdRoll, Grindr (Öffnet in neuem Fenster), BT Mobile (Öffnet in neuem Fenster), Samsung (Öffnet in neuem Fenster), OpenX (Öffnet in neuem Fenster), and SITA (Öffnet in neuem Fenster).[23] (Öffnet in neuem Fenster)[24] (Öffnet in neuem Fenster)

Functional programming examples

Factorial

A factorial (Öffnet in neuem Fenster) algorithm implemented in Erlang:

-module(fact). % This is the file 'fact.erl', the module and the filename must match
-export([fac/1]). % This exports the function 'fac' of arity 1 (1 parameter, no type, no name)

fac(0) -> 1; % If 0, then return 1, otherwise (note the semicolon ; meaning 'else')
fac(N) when N > 0, is_integer(N) -> N * fac(N-1).
% Recursively determine, then return the result
% (note the period . meaning 'endif' or 'function end')
%% This function will crash if anything other than a nonnegative integer is given.
%% It illustrates the "Let it crash" philosophy of Erlang.

Fibonacci sequence

A tail recursive algorithm that produces the Fibonacci sequence (Öffnet in neuem Fenster):

%% The module declaration must match the file name "series.erl" 
-module(series).

%% The export statement contains a list of all those functions that form
%% the module's public API.  In this case, this module exposes a single
%% function called fib that takes 1 argument (I.E. has an arity of 1)
%% The general syntax for -export is a list containing the name and
%% arity of each public function
-export([fib/1]).

%% ---------------------------------------------------------------------
%% Public API
%% ---------------------------------------------------------------------

%% Handle cases in which fib/1 receives specific values
%% The order in which these function signatures are declared is a vital
%% part of this module's functionality

%% If fib/1 receives a negative number, then return the atom err_neg_val
%% Normally, such defensive coding is discouraged due to Erlang's 'Let
%% it Crash' philosophy, but here the result would be an infinite loop.
fib(N) when N < 0 -> err_neg_val;

%% If fib/1 is passed precisely the integer 0, then return 0
fib(0) -> 0;

%% For all other values, call the private function fib_int/3 to perform
%% the calculation
fib(N) -> fib_int(N-1, 0, 1).

%% ---------------------------------------------------------------------
%% Private API
%% ---------------------------------------------------------------------

%% If fib_int/3 receives 0 as its first argument, then we're done, so
%% return the value in argument B. The second argument is denoted _ to
%% disregard its value.
fib_int(0, _, B) -> B;

%% For all other argument combinations, recursively call fib_int/3
%% where each call does the following:
%%  - decrement counter N
%%  - pass the third argument as the new second argument
%%  - pass the sum of the second and third arguments as the new
%%    third argument
fib_int(N, A, B) -> fib_int(N-1, B, A+B).

Omitting the comments gives a much shorter program.

-module(series).
-export([fib/1]).

fib(N) when N < 0 -> err_neg_val;
fib(0) -> 0;
fib(N) -> fib_int(N-1, 0, 1).

fib_int(0, _, B) -> B;
fib_int(N, A, B) -> fib_int(N-1, B, A+B).

Quicksort

Quicksort (Öffnet in neuem Fenster) in Erlang, using list comprehension (Öffnet in neuem Fenster):[25] (Öffnet in neuem Fenster)

%% qsort:qsort(List)
%% Sort a list of items
-module(qsort).     % This is the file 'qsort.erl'
-export([qsort/1]). % A function 'qsort' with 1 parameter is exported (no type, no name)

qsort([]) -> []; % If the list [] is empty, return an empty list (nothing to sort)
qsort([Pivot|Rest]) ->
    % Compose recursively a list with 'Front' for all elements that should be before 'Pivot'
    % then 'Pivot' then 'Back' for all elements that should be after 'Pivot'
    qsort([Front || Front <- Rest, Front < Pivot]) ++ 
    [Pivot] ++
    qsort([Back || Back <- Rest, Back >= Pivot]).

The above example recursively invokes the function qsort until nothing remains to be sorted. The expression [Front || Front <- Rest, Front < Pivot] is a list comprehension (Öffnet in neuem Fenster), meaning "Construct a list of elements Front such that Front is a member of Rest, and Front is less than Pivot." ++ is the list concatenation operator.

A comparison function can be used for more complicated structures for the sake of readability.

The following code would sort lists according to length:

% This is file 'listsort.erl' (the compiler is made this way)
-module(listsort).
% Export 'by_length' with 1 parameter (don't care about the type and name)
-export([by_length/1]).

by_length(Lists) -> % Use 'qsort/2' and provides an anonymous function as a parameter
   qsort(Lists, fun(A,B) -> length(A) < length(B) end).

qsort([], _)-> []; % If list is empty, return an empty list (ignore the second parameter)
qsort([Pivot|Rest], Smaller) ->
    % Partition list with 'Smaller' elements in front of 'Pivot' and not-'Smaller' elements
    % after 'Pivot' and sort the sublists.
    qsort([X || X <- Rest, Smaller(X,Pivot)], Smaller)
    ++ [Pivot] ++
    qsort([Y || Y <- Rest, not(Smaller(Y, Pivot))], Smaller).

A Pivot is taken from the first parameter given to qsort() and the rest of Lists is named Rest. Note that the expression

[X || X <- Rest, Smaller(X,Pivot)]

is no different in form from

[Front || Front <- Rest, Front < Pivot]

(in the previous example) except for the use of a comparison function in the last part, saying "Construct a list of elements X such that X is a member of Rest, and Smaller is true", with Smaller being defined earlier as

fun(A,B) -> length(A) < length(B) end

The anonymous function (Öffnet in neuem Fenster) is named Smaller in the parameter list of the second definition of qsort so that it can be referenced by that name within that function. It is not named in the first definition of qsort, which deals with the base case of an empty list and thus has no need of this function, let alone a name for it.

Data types

Erlang has eight primitive data types (Öffnet in neuem Fenster):

Integers

Integers are written as sequences of decimal digits, for example, 12, 12375 and -23427 are integers. Integer arithmetic is exact and only limited by available memory on the machine. (This is called arbitrary-precision arithmetic (Öffnet in neuem Fenster).)

Atoms

Atoms are used within a program to denote distinguished values. They are written as strings of consecutive alphanumeric characters, the first character being lowercase. Atoms can contain any character if they are enclosed within single quotes and an escape convention exists which allows any character to be used within an atom. Atoms are never garbage collected and should be used with caution, especially if using dynamic atom generation.

Floats

Floating point numbers use the IEEE 754 64-bit representation (Öffnet in neuem Fenster).

References

References are globally unique symbols whose only property is that they can be compared for equality. They are created by evaluating the Erlang primitive make_ref().

Binaries

A binary is a sequence of bytes. Binaries provide a space-efficient way of storing binary data. Erlang primitives exist for composing and decomposing binaries and for efficient input/output of binaries.

Pids

Pid is short for process identifier – a Pid is created by the Erlang primitive spawn(...) Pids are references to Erlang processes.

Ports

Ports are used to communicate with the external world. Ports are created with the built-in function open_port. Messages can be sent to and received from ports, but these messages must obey the so-called "port protocol."

Funs

Funs are function closures (Öffnet in neuem Fenster). Funs are created by expressions of the form: fun(...) -> ... end.

And three compound data types:

Tuples

Tuples are containers for a fixed number of Erlang data types. The syntax {D1,D2,...,Dn} denotes a tuple whose arguments are D1, D2, ... Dn. The arguments can be primitive data types or compound data types. Any element of a tuple can be accessed in constant time.

Lists

Lists are containers for a variable number of Erlang data types. The syntax [Dh|Dt] denotes a list whose first element is Dh, and whose remaining elements are the list Dt. The syntax [] denotes an empty list. The syntax [D1,D2,..,Dn] is short for [D1|[D2|..|[Dn|[]]]]. The first element of a list can be accessed in constant time. The first element of a list is called the head of the list. The remainder of a list when its head has been removed is called the tail of the list.

Maps

Maps contain a variable number of key-value associations. The syntax is#{Key1=>Value1,...,KeyN=>ValueN}.

Two forms of syntactic sugar (Öffnet in neuem Fenster) are provided:

Strings

Strings are written as doubly quoted lists of characters. This is syntactic sugar for a list of the integer Unicode (Öffnet in neuem Fenster) code points for the characters in the string. Thus, for example, the string "cat" is shorthand for [99,97,116].[26] (Öffnet in neuem Fenster)

Records

Records provide a convenient way for associating a tag with each of the elements in a tuple. This allows one to refer to an element of a tuple by name and not by position. A pre-compiler takes the record definition and replaces it with the appropriate tuple reference.

Erlang has no method to define classes, although there are external libraries (Öffnet in neuem Fenster) available.[27] (Öffnet in neuem Fenster)

"Let it crash" coding style

Erlang is designed with a mechanism that makes it easy for external processes to monitor for crashes (or hardware failures), rather than an in-process mechanism like exception handling (Öffnet in neuem Fenster) used in many other programming languages. Crashes are reported like other messages, which is the only way processes can communicate with each other,[28] (Öffnet in neuem Fenster) and subprocesses can be spawned cheaply (see below (Öffnet in neuem Fenster)). The "let it crash" philosophy prefers that a process be completely restarted rather than trying to recover from a serious failure.[29] (Öffnet in neuem Fenster) Though it still requires handling of errors, this philosophy results in less code devoted to defensive programming (Öffnet in neuem Fenster) where error-handling code is highly contextual and specific.[28] (Öffnet in neuem Fenster)

Supervisor trees

A typical Erlang application is written in the form of a supervisor tree. This architecture is based on a hierarchy of processes in which the top level process is known as a "supervisor". The supervisor then spawns multiple child processes that act either as workers or more, lower level supervisors. Such hierarchies can exist to arbitrary depths and have proven to provide a highly scalable and fault-tolerant environment within which application functionality can be implemented.

Within a supervisor tree, all supervisor processes are responsible for managing the lifecycle of their child processes, and this includes handling situations in which those child processes crash. Any process can become a supervisor by first spawning a child process, then calling erlang:monitor/2 on that process. If the monitored process then crashes, the supervisor will receive a message containing a tuple whose first member is the atom 'DOWN'. The supervisor is responsible firstly for listening for such messages and for taking the appropriate action to correct the error condition.

Concurrency and distribution orientation

Erlang's main strength is support for concurrency (Öffnet in neuem Fenster). It has a small but powerful set of primitives to create processes and communicate among them. Erlang is conceptually similar to the language occam (Öffnet in neuem Fenster), though it recasts the ideas of communicating sequential processes (Öffnet in neuem Fenster) (CSP) in a functional framework and uses asynchronous message passing.[30] (Öffnet in neuem Fenster) Processes are the primary means to structure an Erlang application. They are neither operating system (Öffnet in neuem Fenster) processes (Öffnet in neuem Fenster) nor threads (Öffnet in neuem Fenster), but lightweight processes (Öffnet in neuem Fenster) that are scheduled by BEAM. Like operating system processes (but unlike operating system threads), they share no state with each other. The estimated minimal overhead for each is 300 words (Öffnet in neuem Fenster).[31] (Öffnet in neuem Fenster) Thus, many processes can be created without degrading performance. In 2005, a benchmark with 20 million processes was successfully performed with 64-bit Erlang on a machine with 16 GB random-access memory (Öffnet in neuem Fenster) (RAM; total 800 bytes/process).[32] (Öffnet in neuem Fenster) Erlang has supported symmetric multiprocessing (Öffnet in neuem Fenster) since release R11B of May 2006.

While threads (Öffnet in neuem Fenster) need external library support in most languages, Erlang provides language-level features to create and manage processes with the goal of simplifying concurrent programming. Though all concurrency is explicit in Erlang, processes communicate using message passing (Öffnet in neuem Fenster) instead of shared variables, which removes the need for explicit locks (Öffnet in neuem Fenster) (a locking scheme is still used internally by the VM).[33] (Öffnet in neuem Fenster)

Inter-process communication (Öffnet in neuem Fenster) works via a shared-nothing (Öffnet in neuem Fenster) asynchronous (Öffnet in neuem Fenster) message passing (Öffnet in neuem Fenster) system: every process has a "mailbox", a queue (Öffnet in neuem Fenster) of messages that have been sent by other processes and not yet consumed. A process uses the receive primitive to retrieve messages that match desired patterns. A message-handling routine tests messages in turn against each pattern, until one of them matches. When the message is consumed and removed from the mailbox the process resumes execution. A message may comprise any Erlang structure, including primitives (integers, floats, characters, atoms), tuples, lists, and functions.

The code example below shows the built-in support for distributed processes:

 % Create a process and invoke the function web:start_server(Port, MaxConnections)
 ServerProcess = spawn(web, start_server, [Port, MaxConnections]),

 % Create a remote process and invoke the function
 % web:start_server(Port, MaxConnections) on machine RemoteNode
 RemoteProcess = spawn(RemoteNode, web, start_server, [Port, MaxConnections]),

 % Send a message to ServerProcess (asynchronously). The message consists of a tuple
 % with the atom "pause" and the number "10".
 ServerProcess ! {pause, 10},

 % Receive messages sent to this process
 receive
         a_message -> do_something;
         {data, DataContent} -> handle(DataContent);
         {hello, Text} -> io:format("Got hello message: ~s", [Text]);
         {goodbye, Text} -> io:format("Got goodbye message: ~s", [Text])
 end.

As the example shows, processes may be created on remote nodes, and communication with them is transparent in the sense that communication with remote processes works exactly as communication with local processes.

Concurrency supports the primary method of error-handling in Erlang. When a process crashes, it neatly exits and sends a message to the controlling process which can then take action, such as starting a new process that takes over the old process's task.[34] (Öffnet in neuem Fenster)[35] (Öffnet in neuem Fenster)

Implementation

The official reference implementation of Erlang uses BEAM (Öffnet in neuem Fenster).[36] (Öffnet in neuem Fenster) BEAM is included in the official distribution of Erlang, called Erlang/OTP. BEAM executes bytecode (Öffnet in neuem Fenster) which is converted to threaded code (Öffnet in neuem Fenster) at load time. It also includes a native code compiler on most platforms, developed by the High Performance Erlang Project (HiPE) at Uppsala University (Öffnet in neuem Fenster). Since October 2001 the HiPE system is fully integrated in Ericsson's Open Source Erlang/OTP system.[37] (Öffnet in neuem Fenster) It also supports interpreting, directly from source code via abstract syntax tree (Öffnet in neuem Fenster), via script as of R11B-5 release of Erlang.

Hot code loading and modules

Erlang supports language-level Dynamic Software Updating (Öffnet in neuem Fenster). To implement this, code is loaded and managed as "module" units; the module is a compilation unit (Öffnet in neuem Fenster). The system can keep two versions of a module in memory at the same time, and processes can concurrently run code from each. The versions are referred to as the "new" and the "old" version. A process will not move into the new version until it makes an external call to its module.

An example of the mechanism of hot code loading:

  %% A process whose only job is to keep a counter.
  %% First version
  -module(counter).
  -export([start/0, codeswitch/1]).

  start() -> loop(0).

  loop(Sum) ->
    receive
       {increment, Count} ->
          loop(Sum+Count);
       {counter, Pid} ->
          Pid ! {counter, Sum},
          loop(Sum);
       code_switch ->
          ?MODULE:codeswitch(Sum)
          % Force the use of 'codeswitch/1' from the latest MODULE version
    end.

  codeswitch(Sum) -> loop(Sum).

For the second version, we add the possibility to reset the count to zero.

  %% Second version
  -module(counter).
  -export([start/0, codeswitch/1]).

  start() -> loop(0).

  loop(Sum) ->
    receive
       {increment, Count} ->
          loop(Sum+Count);
       reset ->
          loop(0);
       {counter, Pid} ->
          Pid ! {counter, Sum},
          loop(Sum);
       code_switch ->
          ?MODULE:codeswitch(Sum)
    end.

  codeswitch(Sum) -> loop(Sum).

Only when receiving a message consisting of the atom code_switch will the loop execute an external call to codeswitch/1 (?MODULE is a preprocessor macro for the current module). If there is a new version of the counter module in memory, then its codeswitch/1 function will be called. The practice of having a specific entry-point into a new version allows the programmer to transform state to what is needed in the newer version. In the example, the state is kept as an integer.

In practice, systems are built up using design principles from the Open Telecom Platform, which leads to more code upgradable designs. Successful hot code loading is exacting. Code must be written with care to make use of Erlang's facilities.

Distribution

In 1998, Ericsson released Erlang as free and open-source software (Öffnet in neuem Fenster) to ensure its independence from a single vendor and to increase awareness of the language. Erlang, together with libraries and the real-time distributed database Mnesia (Öffnet in neuem Fenster), forms the OTP collection of libraries. Ericsson and a few other companies support Erlang commercially.

Since the open source release, Erlang has been used by several firms worldwide, including Nortel (Öffnet in neuem Fenster) and Deutsche Telekom (Öffnet in neuem Fenster).[38] (Öffnet in neuem Fenster) Although Erlang was designed to fill a niche and has remained an obscure language for most of its existence, its popularity is growing due to demand for concurrent services.[39] (Öffnet in neuem Fenster)[40] (Öffnet in neuem Fenster) Erlang has found some use in fielding massively multiplayer online role-playing game (Öffnet in neuem Fenster) (MMORPG) servers.[41] (Öffnet in neuem Fenster)

See also

References

  1. Clarke, Gavin (5 February 2011). "Battlestar Galactica vets needed for online roleplay" (Öffnet in neuem Fenster). Music and Media. The Reg (Öffnet in neuem Fenster). Retrieved 8 February 2011.

Further reading

External links

(Öffnet in neuem Fenster)

Wikimedia Commons has media related to Erlang (programming language) (Öffnet in neuem Fenster).

(Öffnet in neuem Fenster)

Wikibooks has a book on the topic of: Erlang Programming (Öffnet in neuem Fenster)

0 Kommentare

Möchtest du den ersten Kommentar schreiben?
Werde Mitglied von Jie's stick figures und starte die Unterhaltung.
Mitglied werden