概要
paiza.ioでelixirやってみた。
Enum.reduce使ってみた。
サンプルコード
c = 140
l = 4
b = "fizz"
s = String.split(b, "", trim: true)
#IO.inspect(s)
str = Enum.reduce(s, ["", c], fn x, y ->
pc = Enum.at(y, 1)
str = Enum.at(y, 0) <>
" #{pc}:\n" <>
" begin\n" <>
" out <= \"#{x}\";\n" <>
" pc <= #{pc + 1};\n" <>
" end\n"
pc = pc + 1
[str, pc]
end)
str = Enum.at(str, 0) <>
" #{c + l}:\n" <>
" begin\n" <>
" out <= \" \";\n" <>
" pc <= #{c + 10};\n" <>
" end\n"
IO.puts str
実行結果
140:
begin
out <= "f";
pc <= 141;
end
141:
begin
out <= "i";
pc <= 142;
end
142:
begin
out <= "z";
pc <= 143;
end
143:
begin
out <= "z";
pc <= 144;
end
144:
begin
out <= " ";
pc <= 150;
end
成果物
以上。