Skip to content

Instantly share code, notes, and snippets.

@shiro01
Created September 19, 2018 10:20
Show Gist options
  • Save shiro01/7f3bf226df0aa9420d81552fc3aa80d5 to your computer and use it in GitHub Desktop.
Save shiro01/7f3bf226df0aa9420d81552fc3aa80d5 to your computer and use it in GitHub Desktop.
WandboxでElixirを勉強した時のログ
defmodule Wandbox do
def hello() do
IO.write "Hello, "
IO.puts "World, Wandbox!"
IO.inspect "debug"
IO.inspect Foo.x
IO.puts Foo.x
IO.inspect("hoge")
IO.puts "-----"
a = "hello"
IO.puts a
a = "hello hello" # 上書き可能
IO.puts a
# ^a = "word word" # ^をつけて再代入防止
IO.puts "-----"
num = 1
IO.puts num
num = 1.234
IO.puts num
num = 100_000_000
IO.puts num
IO.puts "-----"
num = 1 + 1
IO.puts num
num = 1 - 1
IO.puts num
num = 1 * 1
IO.puts num
num = 1 / 1
IO.puts num
num = 5.0 / 2
IO.puts num
IO.puts "-----"
str1 = 'abc'
IO.puts str1
str2 = "def"
IO.puts str2
str3 = "a\taa\baa\naa"
IO.puts str3
str4 = "#{str1} def"
IO.puts str4
IO.puts "-----"
str1 = "aaa" <> "bbb"
str1 = str1 <> "aaa" <> "bbb"
IO.puts str1
str2 = Enum.join(["aaa", "bbb", "ccc"], ",")
IO.puts str2
IO.puts "-----"
record = String.split("ddd,eee,fff", ~r/,/)
IO.puts record
IO.puts "-----"
length = String.length("abcdef")
IO.puts length
IO.puts "-----"
substr = String.slice("abcd", 0, 2)
IO.puts substr
substr = String.slice("abcd", 1..3)
IO.puts substr
IO.puts "-----"
[{idx, _}] = Regex.run(~r/b/, "abcd", return: :index)
IO.puts idx
[{idx, _}] = Regex.run(~r/cd/, "abcd", return: :index)
IO.puts idx
IO.puts "-----"
ary = [100, 200, 300]
IO.inspect ary
a = Enum.at(ary, 0)
IO.puts a
b = Enum.at(ary, 1)
IO.puts b
IO.inspect List.insert_at(ary, 0, 1)
IO.inspect List.insert_at(ary, 0, 2)
IO.puts "-----"
ary = [1, 2, 3]
[a|tail] = ary
IO.inspect a
IO.inspect tail
IO.inspect [5|ary]
IO.inspect List.last(ary)
IO.inspect List.insert_at(ary, -1, 9)
IO.puts "-----"
map = %{a: 1, b: 2}
IO.inspect map
IO.inspect map[:a]
IO.inspect map[:b]
IO.inspect Map.put(map, :c, 5)
IO.inspect Map.put(map, :d, 7)
IO.inspect map
IO.inspect Map.keys(map)
IO.inspect Map.values(map)
IO.inspect Map.has_key?(map, :a)
IO.inspect Map.delete(map, :a)
IO.inspect map # deleteしても素はそのまま
IO.puts "-----"
IO.inspect a
if a == 1 do # false ,nill 以外はすべてtrue
IO.inspect "true"
else
IO.inspect "false"
end
if a < 0 do
IO.inspect "true"
else
IO.inspect "false"
end
unless a < 0 do
IO.inspect "true"
else
IO.inspect "false"
end
IO.puts "-----"
My.loop(5)
IO.puts "-----"
Enum.each([1,2,3], fn(i) ->
IO.puts(i)
end)
Enum.each([1,2,3], &IO.puts/1)
l = [1,2,3,4,5]
IO.inspect l
IO.inspect Enum.filter(l, &rem(&1,2) == 0)
IO.inspect Enum.reject(l, &rem(&1,2) == 0)
IO.inspect Enum.find(l, &rem(&1,2) == 0)
IO.inspect rem(10,2) == 0 # あまり
IO.inspect Enum.member?(l,3)
IO.inspect Enum.member?(l,6)
IO.inspect Enum.all?(l, &rem(&1, 2) ==0)
l2 = [2,4,6]
IO.inspect Enum.all?(l2, &rem(&1, 2) ==0)
IO.inspect Enum.count(l, &rem(&1, 2) ==0)
IO.inspect Enum.max(l)
IO.inspect Enum.min(l)
IO.inspect abs(-10)
IO.inspect abs(10)
l3 = [1,-2,3,4,-5]
IO.inspect Enum.max_by(l3, &abs/1)
IO.inspect Enum.min_by(l3, &abs/1)
IO.inspect Enum.sort(l3)
IO.inspect Enum.sort_by(l3, &abs/1)
IO.inspect Enum.map(l3, &abs/1)
IO.puts "-----"
IO.inspect Bar.sum(1,2)
IO.puts "-----"
x = 1
if x == 1, do: IO.puts "foo"
add = fn a, b -> a + b end
IO.inspect add.(1, 2)
IO.inspect {:ok, "hello"}
IO.puts "-----"
IO.inspect l
|> Enum.filter(fn e -> e < 4 end)
|> Enum.map(fn e -> e * 10 end)
IO.inspect l
|> Enum.filter(&(&1 < 4))
|> Enum.map(&(&1 * 10))
IO.puts "-----"
IO.inspect sample = 1
IO.inspect 1 = sample
# IO.inspect 2 = sample # error になる
IO.inspect sample = 2
IO.inspect sample2 = [1,2,3]
[el1, el2, el3] = sample2
IO.inspect sample2 = [1,2,3]
end
def hoge(h) do
IO.puts h
end
def foreach
end
defmodule Bar do
def sum(x, y) do
x + y
end
end
defmodule My do
def loop(0) do
IO.puts "done"
end
def loop(times) do
IO.puts "#{times} times remaining"
loop(times - 1)
end
end
defmodule Foo do
@val "hoge"
def x do
@val
end
end
Wandbox.hello()
Wandbox.hoge("def test")
# ログ
#
# Start
# warning: implementation not provided for predefined def foreach/0
# prog.exs:194
#
# warning: variable "sample" is unused
# prog.exs:183
#
# warning: variable "el1" is unused
# prog.exs:186
#
# warning: variable "el2" is unused
# prog.exs:186
#
# warning: variable "el3" is unused
# prog.exs:186
#
# warning: variable "sample2" is unused
# prog.exs:187
#
# Hello, World, Wandbox!
# "debug"
# "hoge"
# hoge
# "hoge"
# -----
# hello
# hello hello
# -----
# 1
# 1.234
# 100000000
# -----
# 2
# 0
# 1
# 1.0
# 2.5
# -----
# abc
# def
# a aaaa
# aa
# abc def
# -----
# aaabbbaaabbb
# aaa,bbb,ccc
# -----
# dddeeefff
# -----
# 6
# -----
# ab
# bcd
# -----
# 1
# 2
# -----
# [100, 200, 300]
# 100
# 200
# [1, 100, 200, 300]
# [2, 100, 200, 300]
# -----
# 1
# [2, 3]
# [5, 1, 2, 3]
# 3
# [1, 2, 3, 9]
# -----
# %{a: 1, b: 2}
# 1
# 2
# %{a: 1, b: 2, c: 5}
# %{a: 1, b: 2, d: 7}
# %{a: 1, b: 2}
# [:a, :b]
# [1, 2]
# true
# %{b: 2}
# %{a: 1, b: 2}
# -----
# 1
# "true"
# "false"
# "true"
# -----
# 5 times remaining
# 4 times remaining
# 3 times remaining
# 2 times remaining
# 1 times remaining
# done
# -----
# 1
# 2
# 3
# 1
# 2
# 3
# [1, 2, 3, 4, 5]
# [2, 4]
# [1, 3, 5]
# 2
# true
# true
# false
# false
# true
# 2
# 5
# 1
# 10
# 10
# -5
# 1
# [-5, -2, 1, 3, 4]
# [1, -2, 3, 4, -5]
# [1, 2, 3, 4, 5]
# -----
# 3
# -----
# foo
# 3
# {:ok, "hello"}
# -----
# [10, 20, 30]
# [10, 20, 30]
# -----
# 1
# 1
# 2
# [1, 2, 3]
# [1, 2, 3]
# def test
# 0
# Finish
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment