1

次の機能を実装しました。

  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、それともここで何かが完全に欠けていますか?

4

1 に答える 1