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 8

paiza.ioでelixir その83

Last updated at Posted at 2022-11-20

概要

paiza.ioでelixirやってみた。
ローパスフィルターが、上手く動かない。
調査してみた。

問題点

src = [1, 2, 3]
v = 0
Enum.map(src, fn x -> 
    IO.puts(v)
    v = x
end)
  • 自分の期待値は、0,1,2ですが、結果は0,0,0です。

解決策

  • Agentを使いました。
defmodule Out2 do
	use Agent
	def start_link() do
		Agent.start_link(fn -> 
			0 
		end, name: __MODULE__)
	end
	def get() do
		Agent.get(__MODULE__, fn v ->
			v
		end)
	end
	def put(o) do
	    Agent.update(__MODULE__, fn v ->
			o
		end)
    end
end

Out2.start_link
src = [1, 2, 3]

Enum.map(src, fn x -> 
    v = Out2.get
    IO.puts(v)
    Out2.put(x)
end)

成果物

以上。

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?