Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Elixir:IO.read从终端退出_Elixir - Fatal编程技术网

Elixir:IO.read从终端退出

Elixir:IO.read从终端退出,elixir,Elixir,我正在尝试使用IO读取输入。read:stdio,:逐行读取,我能够在终端中输入,但在提供输入后无法退出 defmodule SumOfTwo do def main() do IO.read(:stdio, :all) |> String.split() |> Enum.reduce(fn x, acc -> String.to_integer(acc) + String.to_integer(x) end) |> IO.puts()

我正在尝试使用IO读取输入。read:stdio,:逐行读取,我能够在终端中输入,但在提供输入后无法退出

defmodule SumOfTwo do
  def main() do
    IO.read(:stdio, :all)
    |> String.split()
    |> Enum.reduce(fn x, acc -> String.to_integer(acc) + String.to_integer(x) end)
    |> IO.puts()
  end
end
SumOfTwo.main

当您将:all赋给IO.read/2时,它将一直读取,直到获得和EOF文件结尾字符。如果您将管道插入elixir脚本,则会包含一个:

$echo 2 3 4 | elixir main.exs 9 您还可以从终端发送EOF字符。对于Linux终端,这通常是ctrl+d:

$elixir main.es 4. 5. 6. 15 不过,您需要更改reduce函数,因为对integer调用String.to_integer/1将引发错误。我的示例将IO.read/2默认为:stdio

除雾器 def主do :全部 |>读 |>String.split |>Enum.reducefn x,acc->acc+String.to_integerx end |>IO.puts 终止 终止
实际上,正如文档中提到的,如果它发现空字符串,它将作为:EOF处理,但是从终端,它没有检测到。不管怎么说,CTRL+d起作用了。