1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

paiza.ioでelixirAdvent Calendar 2022

Day 17

paiza.ioでelixir その67

Last updated at Posted at 2022-11-13

概要

paiza.ioでelixirやってみた。
kernel使ってみた。

サンプルコード

!!true
|> IO.inspect
!!5
|> IO.inspect

!![1,2]
|> IO.inspect

!!"foo"
|> IO.inspect
1 != 2
|> IO.inspect
1 != 1.0
|> IO.inspect

1 * 2
|> IO.inspect


+1
|> IO.inspect


1 + 2
|> IO.inspect

-2
|> IO.inspect

1 - 2
|> IO.inspect
1 / 2
|> IO.inspect
1 < 2
|> IO.inspect
1 <= 2
|> IO.inspect
1 == 2
|> IO.inspect
1 === 2
|> IO.inspect
1 > 2

|> IO.inspect
1 >= 2
|> IO.inspect
abs(-3.33)
|> IO.inspect
true and false
|> IO.inspect
binary_part("foo", 1, 2)
|> IO.inspect
bit_size(<<433::16, 3::3>>)
|> IO.inspect
byte_size(<<433::16, 3::3>>)
|> IO.inspect
div(5, 2)
|> IO.inspect
tuple = {:foo, :bar, 3}
elem(tuple, 1)
|> IO.inspect
hd([1, 2, 3, 4])
|> IO.inspect
x = 1
x in [1, 2, 3]
|> IO.inspect
is_binary("foo")
|> IO.inspect
is_bitstring("foo")
|> IO.inspect
is_exception(%RuntimeError{})
|> IO.inspect
is_exception(%RuntimeError{}, RuntimeError)
|> IO.inspect
is_function(fn x -> 
    x * 2 
end, 1)
|> IO.inspect
is_nil(1)
|> IO.inspect
is_struct(URI.parse("/"))
|> IO.inspect
is_struct(URI.parse("/"), URI)
|> IO.inspect
length([1, 2, 3, 4, 5, 6, 7, 8, 9])
|> IO.inspect
map_size(%{a: "foo", b: "bar"})
|> IO.inspect
not false
|> IO.inspect
true or false
|> IO.inspect
rem(5, 2)
|> IO.inspect
round(5.6)
|> IO.inspect
tl([1, 2, 3, :go])
|> IO.inspect
trunc(5.4)
|> IO.inspect
tuple_size({:a, :b, :c})
|> IO.inspect
!Enum.empty?([])
|> IO.inspect
!List.first([])
|> IO.inspect
Enum.empty?([]) && Enum.empty?([])
|> IO.inspect
List.first([]) && true
|> IO.inspect
[1] ++ [2, 3]
|> IO.inspect
'foo' ++ 'bar'
|> IO.inspect
[1, 2, 3] -- [1, 2]
|> IO.inspect
0 in 1..3
|> IO.inspect
0 in 1..3//1
|> IO.inspect
"foo" <> "bar"
|> IO.inspect
"abcd" =~ ~r/c(d)/
|> IO.inspect
[1, [2], 3] 
|> List.flatten()
Enum.empty?([1]) || Enum.empty?([1])
|> IO.inspect
apply(fn x -> x * 2 end, [2])
|> IO.inspect
apply(Enum, :reverse, [[1, 2, 3]])
|> IO.inspect
x = 1
binding()
|> IO.inspect
defmodule Foo do
	def bar, do: :baz
end
Foo.bar()
|> IO.inspect
defmodule MyList do
	defdelegate reverse(list), to: Enum
	defdelegate other_reverse(list), to: Enum, as: :reverse
end
MyList.reverse([1, 2, 3])
|> IO.inspect
defmodule MyLogic do
	defmacro unless(expr, opts) do
		quote do
			if !unquote(expr), unquote(opts)
		end
	end
end
#require MyLogic
#MyLogic.unless false do
#	IO.puts("It works")
#end
#defmacrop(call, expr \\ nil)(macro)
|> IO.inspect
defmodule Number do
	def one, do: 1
	def two, do: 2
end
Number.one()
|> IO.inspect
defmodule DefaultMod do
	defmacro __using__(_opts) do
		quote do
			def test(x, y) do
				x + y
			end
			defoverridable test: 2
		end
	end
end
defmodule InheritMod do
	use DefaultMod
	def test(x, y) do
		x * y + super(x, y)
	end
end
defmodule Behaviour do
	@callback foo :: any
end
defmodule DefaultMod do
	defmacro __using__(_opts) do
		quote do
			@behaviour Behaviour
			def foo do
				"Override me"
			end
			defoverridable Behaviour
		end
	end
end
defmodule InheritMod do
	use DefaultMod
	def foo do
		"Overridden"
	end
end
#defp(call, expr \\ nil)(macro)
defmodule Foo do
	def bar do
		sum(1, 2)
	end
	defp sum(a, b), do: a + b
end
Foo.bar()
|> IO.inspect
destructure([x, y, z], [1, 2, 3, 4, 5])
|> IO.inspect
function_exported?(Enum, :map, 2)
|> IO.inspect
users = %{"john" => %{age: 27}, "meg" => %{age: 23}}
get_and_update_in(users["john"].age, &{&1, &1 + 1})
|> IO.inspect
users = %{"john" => %{age: 27}, "meg" => %{age: 23}}
get_in(users, ["john", :age])

|> IO.inspect
inspect(:foo)

|> IO.inspect
macro_exported?(Kernel, :use, 2)
|> IO.inspect
make_ref()
|> IO.inspect
match?(1, 1)
|> IO.inspect
max(1, 2)
|> IO.inspect
min(1, 2)
|> IO.inspect
users = %{"john" => %{age: 27}, "meg" => %{age: 23}}
pop_in(users["john"][:age])
|> IO.inspect
tuple = {:foo, :bar, 3}
put_elem(tuple, 0, :baz)
|> IO.inspect

send(self(), :hello)

|> IO.inspect
~C(foo)

|> IO.inspect
~D[2015-01-13]
|> IO.inspect
~N[2015-01-13 13:00:07]
|> IO.inspect
Regex.match?(~R(f#{1,3}o), "f#o")
|> IO.inspect
~S(foo)
|> IO.inspect
~T[13:00:07]
|> IO.inspect
~U[2015-01-13 13:00:07Z]
|> IO.inspect
~W(foo #{bar} baz)
|> IO.inspect
current = self()
child = spawn(fn -> 
    send(current, {self(), 1 + 2}) 
end)
receive do
	{^child, 3} -> 
	    IO.puts("Received 3 back")
end
current = self()
child = spawn_link(fn -> 
    send(current, {self(), 1 + 2}) 
end)
receive do
	{^child, 3} -> 
	    IO.puts("Received 3 back")
end
current = self()
spawn_monitor(fn -> 
    send(current, {self(), 1 + 2}) 
end)
defmodule User do
	defstruct name: "john"
end
struct(User)
|> IO.inspect
tap(1, fn x -> x + 1 end)
|> IO.inspect
1 |> then(fn x -> x * 2 end)

|> IO.inspect
to_charlist(:foo)
|> IO.inspect
to_string(:foo)
|> IO.inspect
unless(Enum.empty?([]), do: "Hello")
|> IO.inspect
users = %{"john" => %{age: 27}, "meg" => %{age: 23}}
update_in(users["john"][:age], &(&1 + 1))
|> IO.inspect







実行結果

true
true
true
true
2
1.0
2
1
3
-2
-1
0.5
2
2
2
2
2
2
3.33
false
"oo"
19
3
2
:bar
1
true
true
true
true
true
true
false
true
true
9
2
true
1
6
[2, 3, :go]
5
3
false
true
true
[1, 2, 3]
'foobar'
[3]
false
false
"foobar"
~r/c(d)/
false
4
[3, 2, 1]
[tuple: {:foo, :bar, 3}, x: 1]
:baz
[3, 2, 1]
{:module, MyLogic,
 <<70, 79, 82, 49, 0, 0, 5, 136, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 151,
   0, 0, 0, 16, 14, 69, 108, 105, 120, 105, 114, 46, 77, 121, 76, 111, 103, 105,
   99, 8, 95, 95, 105, 110, 102, 111, 95, ...>>, {:unless, 2}}
1
3
[1, 2, 3]
true
{27, %{"john" => %{age: 28}, "meg" => %{age: 23}}}
27
":foo"
true
#Reference<0.1088263613.1616379906.255837>
true
2
1
{27, %{"john" => %{}, "meg" => %{age: 23}}}
{:baz, :bar, 3}
:hello
'foo'
~D[2015-01-13]
~N[2015-01-13 13:00:07]
true
"foo"
~T[13:00:07]
~U[2015-01-13 13:00:07Z]
["foo", "\#{bar}", "baz"]
Received 3 back
Received 3 back
%User{name: "john"}
1
2
'foo'
"foo"
nil
%{"john" => %{age: 28}, "meg" => %{age: 23}}
×

成果物

以上。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?