2017-10-21 112 views
1

我有一個管道,它看起來像Task.async_stream |> Stream.drop_while中是否有任何順序?

digits 
|> Task.async_stream(__MODULE__, :filter, [s, values], timeout: :infinity) 
|> Stream.drop_while(fn {_, k} -> k == :contradiction or k == [] end) 
|> Stream.take(1) 

有此定義的任何命令?或者只是返回不滿足條件的第一個filter的結果將返回?

回答

1

Task.async_streamhas an option for this:ordered: true | false。如果設置爲true,則結果將與輸入列表的順序相同。如果它是錯誤的,結果將按任務完成的順序排列。該選項的值默認爲true,因此在您的代碼中,結果將與輸入列表的順序相同。

iex(1)> [5, 4, 3, 2, 1] |> Task.async_stream(fn x -> :timer.sleep(x * 100); x end) |> Enum.to_list 
[ok: 5, ok: 4, ok: 3, ok: 2, ok: 1] 
iex(2)> [5, 4, 3, 2, 1] |> Task.async_stream(fn x -> :timer.sleep(x * 100); x end, ordered: false) |> Enum.to_list 
[ok: 1, ok: 2, ok: 3, ok: 4, ok: 5] 
相關問題