日常
Mix.install([
{:vega_lite, "~> 0.1.9"},
{:kino, "~> 0.14.0"},
{:kino_vega_lite, "~> 0.1.13"}
])
chapter 0 - what can we do?
defmodule Parallel do
def pmap(coll, func) do
coll
|> Enum.map(&(Task.async(fn -> func.(&1) end)))
|> Enum.map(&Task.await/1)
end
end
x_values = 1..10
y_values = Parallel.pmap x_values, &(&1 * &1)
data = Enum.zip(x_values, y_values)
|> Enum.map(fn {x, y} -> %{x: x, y: y} end)
plot =
VegaLite.new()
|> VegaLite.data_from_values(data)
|> VegaLite.encode_field(:x, "x", type: :quantitative)
|> VegaLite.encode_field(:y, "y", type: :quantitative)
|> VegaLite.mark(:point)
Kino.VegaLite.new(plot)
chapter 1 - getting our feet wet
"日常" |> String.graphemes() |> Enum.frequencies()
%{"常" => 1, "日" => 1}
IO.puts "Hello, welcome! We are here everyday"
Hello, welcome! We are here everyday
:ok
defmodule Geometry.Rectangle do
def area(a, b) do
a * b
end
def some_other_function(a, b), do: a + b
end
Geometry.Rectangle.area(4, 8)
Geometry.Rectangle.some_other_function(2, 4)
6
-5
|> abs()
|> Integer.to_string()
|> IO.puts()
quote do
-5
|> abs()
|> Integer.to_string()
|> IO.puts()
end
Code.string_to_quoted """
-5
|> abs()
|> Integer.to_string()
|> IO.puts()
"""
IO.puts(Integer.to_string(abs(-5)))
5
5
:ok
defmodule Calculator do
def add(a), do: add(a, 0)
def add(a, b), do: a + b
end
Calculator.add(2, 5)
7
defmodule Circle do
@moduledoc "Implements basic circle functions"
@pi 3.14159
@doc "Computes the area of a circle"
@spec area(number) :: number
def area(r), do: r*r*@pi
@doc "Computes the circumference of a circle"
@spec circumference(number) :: number
def circumference(r), do: 2*r*@pi
end
{:module, Circle, <<70, 79, 82, 49, 0, 0, 7, ...>>, {:circumference, 1}}
AnAtom == :"Elixir.AnAtom"
true