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.

wslでelixir その3

Posted at

概要

wsl(wsl2じゃない)で、elixirやってみた。
練習問題やってみた。

練習問題

zundokoをtestせよ。

参考にしたページ。

サンプルコード

defmodule Zundoko do
	@moduledoc """
	Documentation for `Zundoko`.
	"""
	@doc """
	Hello world.
	## Examples
			iex> Zundoko.hello()
			:world
	"""
	def hello do
		:world
	end
	def auto(), do: Stream.repeatedly(fn -> [ "ズン", "ドコ" ] |> Enum.random end) |> run
	@doc """
	Run Zundoko
	## Examples
		iex> [] |> Zundoko.run
		[]
		iex> [ "" ] |> Zundoko.run
		[ "" ]
		iex> [ "ズン", "" ] |> Zundoko.run
		[ "ズン", "" ]
		iex> [ "ズン", "ズン", "" ] |> Zundoko.run
		[ "ズン", "ズン", "" ]
		iex> [ "ズン", "ズン", "ズン", "" ] |> Zundoko.run
		[ "ズン", "ズン", "ズン", "" ]
		iex> [ "ズン", "ズン", "ズン", "ズン", "" ] |> Zundoko.run
		[ "ズン", "ズン", "ズン", "ズン", "" ]
		iex> [ "ズン", "ズン", "ズン", "ズン", "ズン", "" ] |> Zundoko.run	# no match overrun
		[ "ズン", "ズン", "ズン", "ズン", "ズン", "" ]
		iex> [ "ズン", "ズン", "ズン", "ドコ", "" ] |> Zundoko.run
		[ "ズン", "ズン", "ズン", "ドコ", "" ]
		iex> [ "ズン", "ズン", "ズン", "ズン", "ドコ", "" ] |> Zundoko.run
		[ "ズン", "ズン", "ズン", "ズン", "ドコ", "キ・ヨ・シ!" ]
		iex> [ "ズン", "ズン", "ズン", "ズン", "ドコ", "ズン", "" ] |> Zundoko.run	# match overrun
		[ "ズン", "ズン", "ズン", "ズン", "ドコ", "キ・ヨ・シ!" ]
	"""
	def run(input) do
		match = [ "ズン", "ズン", "ズン", "ズン", "ドコ" ]
		eol = "キ・ヨ・シ!"
		input
		|> Stream.transform([], fn( item, acc ) -> 
			if List.last( acc ) == eol do
				{ :halt, nil }
			else
				if Enum.count( acc ) < Enum.count( match ) do
					{ [ item ], acc ++ [ item ] }
				else
					if acc == match, do: { [ eol ], acc ++ [ eol ] }, else: { [ item ], [ item ] }
				end
			end
		end )
		|> Enum.to_list
	end
end

test結果

$ mix test
Compiling 1 file (.ex)
...

Finished in 0.1 seconds (0.00s async, 0.1s sync)
2 doctests, 1 test, 0 failures

Randomized with seed 936721

実行結果

$ iex -S mix
Erlang/OTP 25 [erts-13.0.4] [source] [64-bit] [smp:2:2] [ds:2:2:10] [async-threads:1] [jit:ns]

Compiling 1 file (.ex)
Generated zundoko app
Interactive Elixir (1.13.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> Zundoko.auto
["ドコ", "ズン", "ドコ", "ドコ", "ドコ", "ドコ", "ドコ", "ズン",
 "ズン", "ドコ", "ズン", "ドコ", "ドコ", "ドコ", "ズン", "ドコ",
 "ズン", "ドコ", "ドコ", "ズン", "ドコ", "ズン", "ズン", "ドコ",
 "ドコ", "ドコ", "ドコ", "ズン", "ズン", "ズン", "ズン", "ズン",
 "ズン", "ドコ", "ズン", "ドコ", "ドコ", "ズン", "ドコ", "ズン",
 "ズン", "ドコ", "ズン", "ドコ", "ズン", "ズン", "ドコ", "ズン",
 "ドコ", "ドコ", ...]
iex(2)> Zundoko.auto
["ズン", "ズン", "ズン", "ドコ", "ドコ", "ドコ", "ズン", "ドコ",
 "ズン", "ズン", "ズン", "ズン", "ズン", "ドコ", "ドコ", "ドコ",
 "ズン", "ズン", "ズン", "ドコ", "ズン", "ズン", "ズン", "ドコ",
 "ドコ", "ドコ", "ズン", "ドコ", "ズン", "ドコ", "ドコ", "ドコ",
 "ズン", "ドコ", "ドコ", "ドコ", "ドコ", "ドコ", "ズン", "ドコ",
 "ドコ", "ズン", "ドコ", "ズン", "ドコ", "ズン", "ドコ", "ドコ",
 "ドコ", "ドコ", ...]
iex(3)> Zundoko.auto
["ドコ", "ズン", "ズン", "ズン", "ドコ", "ズン", "ズン", "ドコ",
 "ドコ", "ズン", "ズン", "ズン", "ドコ", "ズン", "ズン", "ドコ",
 "ズン", "ズン", "ズン", "ドコ", "ズン", "ズン", "ズン", "ドコ",
 "ズン", "ズン", "ズン", "ズン", "ズン", "ズン", "ドコ", "ドコ",
 "ズン", "ズン", "ドコ", "ドコ", "ドコ", "ズン", "ズン", "ズン",
 "ドコ", "ズン", "ドコ", "ズン", "ズン", "ズン", "ズン", "ズン",
 "ドコ", "ズン", ...]
iex(4)> Zundoko.auto
["ドコ", "ドコ", "ドコ", "ズン", "ズン", "ドコ", "ドコ", "ドコ",
 "ズン", "ズン", "ドコ", "ズン", "ドコ", "ドコ", "ズン", "ズン",
 "ズン", "ズン", "ズン", "ズン", "ズン", "ズン", "ズン", "ズン",
 "ドコ", "キ・ヨ・シ!"]
iex(5)>

以上。

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?