我已经实现了以下功能:
def gaussian(center, height, width) do
Stream.iterate(1, &(&1 + 1))
|> Stream.map(fn (x) -> x - center end)
|> Stream.map(fn (x) -> :math.pow(x, 2) end)
|> Stream.map(fn (x) -> -x / (2 * :math.pow(width, 2)) end)
|> Stream.map(fn (x) -> height * :math.exp(x) end)
|> Stream.map(&Kernel.round/1)
|> Stream.take_while(&(&1 > 0))
|> Enum.to_list
end
使用给定的参数,返回一个空列表:
iex> gaussian(10, 10, 3)
[]
删除Stream.take_while/2
def gaussian(center, height, width) do
Stream.iterate(1, &(&1 + 1))
|> Stream.map(fn (x) -> x - center end)
|> Stream.map(fn (x) -> :math.pow(x, 2) end)
|> Stream.map(fn (x) -> -x / (2 * :math.pow(width, 2)) end)
|> Stream.map(fn (x) -> height * :math.exp(x) end)
|> Stream.map(&Kernel.round/1)
#|> Stream.take_while(&(&1 > 0))
#|> Enum.to_list
|> Enum.take(20)
end
但是给出了这个:
iex> gaussian(10, 10, 3)
[0, 0, 1, 1, 2, 4, 6, 8, 9, 10, 9, 8, 6, 4, 2, 1, 1, 0, 0, 0]
我的电话有什么问题Stream.take_while/2
,还是我在这里完全错过了什么?