Elixir ist eine dynamische, funktionale Sprache zum Erstellen skalierbarer und wartbarer Anwendungen. Elixir nutzt die Erlang VM, die dafür bekannt ist, verteilte und fehlertolerante Systeme mit geringer Latenz auszuführen.
Außerdem wird Elixir wird erfolgreich in Webentwicklung, eingebetteter Software, Datenaufnahme und Multimediaverarbeitung in einer Vielzahl von Branchen eingesetzt.
Hier folge eine kleine Einführung in die Programmiersprache Elixir
Grundlegende Datentypen
Elixir supports the basic data types that other programming languages have. You can use integers (whole numbers), floats (decimal numbers), booleans (true or false) and strings (which are wrapped in double quotes, are UTF-8 encoded, and support line breaks).
Probieren wir in der Elixit Shell einfach die nachfolgenden Beispiele aus.
Atome sind eine besondere Art von Datentyp, die Konstanten (unveränderlichen Variablen) ähneln, ihr einziger Wert jedoch der ihnen gegebene Name ist.
iex(1)> :my_atom
:my_atom
iex(2)> :my_atom == "my_atom"
false
iex(3)> :my_atom == true
false
iex(4)> :my_atom == :my_atom
true
iex(5)> :my_other_atom = 4
** (MatchError) no match of right hand side value: 4
Im letzten Beispiel sehen wir, dass wir einem Atom keinen bestimmten Wert zuweisen können. Auch hier ist ihr einziger Wert ihr Name!
Lists
Listen in Elixir können jeden Datentyp enthalten und beliebig lang sein. Sie können mit ++ zwei Listen verketten oder mit — eine Liste von einer anderen subtrahieren.
Beachten Sie, dass Elixir eine unveränderliche Sprache (immutable language) ist.
Wenn Sie diese Vorgänge ausführen, wird die ursprüngliche Liste also nie geändert. Stattdessen wird eine neue Liste zurückgegeben, die Sie in einer Variablen speichern können:
Listen in Elixir können auch in zwei Teile aufgeteilt werden: den Kopf (head, der mit der hd-Funktion extrahiert werden kann) und den Schwanz (tail , der mit der tl-Funktion extrahiert werden kann). Der Kopf ist das erste Element der Liste und der Schwanz ist der Rest:
Tupel ähneln Listen, aber sie speichern ihre Elemente zusammenhängend im Speicher. Dadurch wird der Zugriff auf Tupelelemente schneller als auf Listenelemente, jedoch ist deren Änderung langsamer.
Sie können ein Tupel als eine Ansammlung von Werten sehen, um eine Art Ressource zu bilden, während Listen verwendet werden, um Dinge aufzuzählen.
iex(7)> {"Tristan", 24}
{"Tristan", 24}
Sie werden feststellen, dass viele Elixir-Funktionen Tupel zurückgeben, um zwischen Erfolgen und Misserfolgen zu unterscheiden, z. B.: {:ok, value} oder {:error, error_message}
Maps
Karten (Maps) sind der Schlüsselwertspeicher (key-value store) von Elixir. Die Schlüssel können einen beliebigen Typ haben und zeigen mit => auf einen Wert.
Most of the time, you’ll probably want the keys in your map to be atoms, like in the example above. When that’s the case, there’s a nice shorthand syntax where we can use a colon instead of “=>” to point to the values, and omit the :-character at the beginning of our atoms:
iex(3)> company_phones =
Structs
Eine Struktur (struct) ist einer Map sehr ähnlich, bietet jedoch einige zusätzliche Funktionen – Sie können die Anzahl der Schlüssel begrenzen und ihnen Standardwerte zuweisen.
Um Strukturen (structs) auszuprobieren, müssen wir eine Elixir-Datei erstellen, also fahren Sie fort und erstellen Sie eine neue Datei namens user.ex. Darin werden wir ein neues Modul definieren, in dem wir auch unsere Struktur definieren:
defmodule User do
defstruct name: "Tristan", age: 24
end
Gehen Sie nun zurück zur IEx-Shell und führen Sie den Befehl c(“user.ex”) aus.
Sie können jetzt eine neue Benutzerstruktur erstellen, indem Sie
❯ iex.bat -S mix
Compiling 1 file (.ex)
Interactive Elixir (1.12.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> c("user.ex")
[User]
iex(2)>
iex(3)>
Conditionals
Die grundlegenden if- und else-Anweisungen funktionieren genau so, wie Sie es erwarten. Beachten Sie, dass wir end verwenden müssen, um anzugeben, wo die if-Anweisungen enden.
iex(4)> if 5 > 4 do
...(4)> "It's higher!"
...(4)> else
...(4)> "It's lower"
...(4)> end
"It's higher!"
Sie können sogar die Umkehrung unless verwenden, um Ihren Code lesbarer zu machen:
iex(6)> unless 5 > 6 do
...(6)> "The laws of mathematics still work!"
...(6)> end
"The laws of mathematics still work!"
Was ist mit “else if”?
In Elixir gibt es kein “else if”. Wenn Sie einen Flow mit mehr als nur zwei Ergebnissen haben, sollten Sie stattdessen wahrscheinlich cond verwenden:
iex(5)> cond do
...(5)> 5 > 6 ->
...(5)> "This isn't true"
...(5)> 5 == 8 ->
...(5)> "Neither is this"
...(5)> true ->
...(5)> "But this is!"
...(5)> end
"But this is!"
Wenn in Ihrer cond-Anweisung nichts als wahr ausgewertet wird, wird eine Ausnahme ausgelöst! Daher ist es normalerweise eine gute Praxis, immer eine letzte true-Bedingung ganz am Ende Ihrer Anweisung hinzuzufügen (wie wir es in unserem obigen Beispiel getan haben), um eventuelle Ausnahmen zu behandeln.
Schließlich gibt es noch eine case-Anweisung, die den Kontrollfluss in Elixir behandelt, aber dazu werden wir im nächsten Kapitel mehr erfahren …
Erstellen einer App
Im ersten Schritt erstellen wir die grundlegende Elixir Anwendung mit Hilfe des Komandos mix.
❯ mix new starterapp
* creating README.md
* creating .formatter.exs
* creating .gitignore
* creating mix.exs
* creating lib
* creating lib/starterapp.ex
* creating test
* creating test/test_helper.exs
* creating test/starterapp_test.exs
Your Mix project was created successfully.
You can use "mix" to compile it, test it, and more:
cd starterapp
mix test
Run "mix help" for more commands.
Durch dieses Kommando wird der Ordner starterapp mit der folgenbden Verzeichnisstruktur erstellt
Ersten Test starten
❯ cd starterapp
❯ mix test
Compiling 1 file (.ex)
Generated starterapp app
..
Finished in 0.07 seconds (0.00s async, 0.07s sync)
1 doctest, 1 test, 0 failures
Randomized with seed 697266
Ersten Programmcode schreiben
Funktion say_hello
defmodule StarterApp do
def hello do
:world
end
def say_hello do
IO.puts("Hello World")
end
end
Interactive Shell mit unserer App starten
iex -S mix
Unter Poweshell gibt es bereits einen Alias iex. Hier nutzen wir diesen Aufruf
iex.bat -S mix
❯ iex.bat -S mix
Compiling 1 file (.ex)
Generated starterapp app
Interactive Elixir (1.12.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)>
Funktion mit mehrerer, auch optionalen Parametern festlegen
defmodule StarterApp do
def main() do
name = IO.gets("Bitte geben Sie Ihrem Name an: ") |> String.trim
say(name)
end
def say("") do
IO.puts "Sie muessen einen Namen angeben!"
main()
end
def say(name) do
IO.puts "Hallo #{name}!"
end
def say(greeting, name) do
IO.puts "#{greeting} #{name}!"
end
end
In der Elixir Shell das Modul neu laden
iex(2)> r StarterApp
warning: redefining module StarterApp (current version defined in memory)
lib/starterapp.ex:1
{:reloaded, StarterApp, [StarterApp]}
iex(3)>
Aufruf ohne Parameter
iex(3)> StarterApp.say()
** (UndefinedFunctionError) function StarterApp.say/0 is undefined or private. Did you mean one of:
* say/1
* say/2
(starterapp 0.1.0) StarterApp.say()
iex(4)>
iex(4)> StarterApp.say("")
Sie muessen einen Namen angeben!
Bitte geben Sie Ihrem Name an: World
Hallo World!
:ok
iex(5)>
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
Cookie
Duration
Description
cookielawinfo-checkbox-analytics
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional
11 months
The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy
11 months
The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Leave a Reply