From: Greg Burri Date: Sat, 25 Apr 2020 14:13:53 +0000 (+0200) Subject: Add some code samples. X-Git-Url: https://git.euphorik.ch/?a=commitdiff_plain;p=Elixir.git Add some code samples. --- diff --git a/sandbox.exs b/sandbox.exs index 58ff0da..baa66cd 100644 --- a/sandbox.exs +++ b/sandbox.exs @@ -18,6 +18,7 @@ IO.inspect b, label: "b" {x, x} = {10, 10} # 'x' must be bind to the same value. IO.inspect x, label: "x" +IO.puts "=== Binary ===" str = "hëllö" <> <<0>> # Append a zero to the string (binary). IO.inspect str, binaries: :as_binaries @@ -31,15 +32,18 @@ IO.inspect(is_bitstring <<0xFF, 0xF::4>>) # true. IO.inspect(x === ?ü) # true. # 'charlist': the string in Erlang. +IO.puts "=== charlist ===" IO.inspect('hëllò', charlists: :as_lists) IO.inspect(to_string('hëllò') == "hëllò") # true. IO.inspect('hëllò' == to_charlist("hëllò")) # true. # keyword list. +IO.puts "=== keyword list ===" IO.inspect([{:a, 1}, {:b, 2}] === [a: 1, b: 2]) # true. IO.inspect(a: 1, b: 2, c: 3) # Brackets omitted. # Maps. +IO.puts "=== Map ===" map = %{:a => 1, 2 => :b} IO.inspect map[:a] # Print 1. IO.inspect map.a # Special syntax with atom key, print 1. @@ -50,10 +54,81 @@ IO.inspect a # Print 1. IO.inspect %{map | 2 => "two"} # Updating 'map'. # Module. +IO.puts "=== Module ===" defmodule Math do def sum(a, b) do a + b end + + def zero?(0), do: true + def zero?(x) when is_integer(x), do: false +end + +IO.inspect Math.sum(1, 2) +IO.inspect Math.zero?(1) + +fun = &Math.zero?/1 # '&' to capture a function as a lambda (and use with high level functions). +IO.inspect fun.(0) # Calling it with same syntaxe as lambdas. + +fun2 = &(&1 + 1) # Shortcut syntax: '&1' is the first parameter. +fun3 = &"Good #{&1}" +IO.inspect fun2.(3) # Print 4. + +# Enum. +IO.puts "=== Enum ===" +IO.inspect Enum.map([1, 2, 3], &(2 * &1)) +IO.inspect Enum.map(%{1 => 2, 3 => 4}, fn {k, v} -> k * v end) +IO.inspect Enum.map(1..10, &(3 * &1)) +IO.inspect Enum.reduce(1..3, 0, &+/2) # 1 + 2 + 3. + +odd? = &(rem(&1, 2) != 0) +IO.inspect 1..100_000 |> Enum.map(&(3 * &1)) |> Enum.filter(odd?) |> Enum.sum + +# Process. +IO.puts "=== Process ===" +pid = + spawn fn -> + receive do + {:hello, sender, msg} -> + IO.puts "Message received: '#{msg}'" + send sender, :ok + end + end + + +send pid, {:hello, self(), "Salut"} +receive do + :ok -> IO.puts "Message delivered!" +after + 1_000 -> IO.puts "Message not delivered :(" +end + +IO.puts "=== State ===" +defmodule KV do # Key-Value. + def stark_link do + Task.start_link(fn -> loop %{} end) + end + + defp loop(map) do + receive do + {:get, key, caller} -> + send caller, Map.get(map, key) + loop map + {:put, key, value} -> + loop Map.put(map, key, value) + end + end +end + +{:ok, kv} = KV.stark_link +send kv, {:put, :a, 42} +send kv, {:put, :b, 420} +send kv, {:get, :b, self()} +receive do + value -> IO.puts "Value received: #{value}" end -IO.inspect Math.sum(1, 2) \ No newline at end of file +IO.puts "=== Agent ===" +{:ok, pid} = Agent.start_link(fn -> %{} end) +Agent.update(pid, fn map -> Map.put(map, :hello, :world) end) +IO.inspect Agent.get(pid, fn map -> Map.get(map, :hello) end) \ No newline at end of file