概要
wsl(wsl2じゃない)で、elixirやってみた。
Livebookで、じゃんけんをDEPLOYしてみた。
写真
localhost:8080/apps/janken
セットアップ
Mix.install([
{:kino, "~> 0.10.0"}
])
サンプルコード
defmodule Janken do
def hand(c) do
cond do
c == 0 ->
"グー"
c == 1 ->
"チョキ"
c == 2 ->
"パー"
:else ->
"うそ"
end
end
def judge(a, b) do
cond do
a == b ->
"あいこ"
a == 0 && b == 1 ->
"あなたの勝ち"
a == 1 && b == 2 ->
"あなたの勝ち"
a == 2 && b == 0 ->
"あなたの勝ち"
a == 0 && b == 2 ->
"あなたの負け"
a == 1 && b == 0 ->
"あなたの負け"
a == 2 && b == 1 ->
"あなたの負け"
end
end
def pon(a) do
res = ""
{a, _} = a
|> Integer.parse()
res = res <> hand(a) <> "と"
b = 0..2
|> Enum.random
res = res <> hand(b) <> "で、"
res = res <> judge(a, b)
res
end
end
frame2 = Kino.Frame.new()
com = Kino.Markdown.new("0-gu 1-choki 2-pa")
Kino.Frame.append(frame2, com)
inputs2 = [ te: Kino.Input.text("Anata")]
form2 = Kino.Control.form(inputs2, submit: "Send", reset_on_submit: [:te])
Kino.listen(form2, fn %{data: %{te: te}, origin: origin} ->
res = Janken.pon(te)
content = Kino.Markdown.new(res)
Kino.Frame.append(frame2, content)
end)
デプロイ
左のロケットマークをクリック、configreをクリック。
以上。