2016-08-03 64 views
1

藥劑的文檔狀態在Elixir中使用drop_while?

drop_while(enumerable, fun) 
    Drops items at the beginning of the enumerable while fun returns a truthy value 

但我很困惑與下面的輸出。這是否意味着一旦得到!truthy一切都被視爲錯誤?

iex> Enum.drop_while([0,1,2,3,4,5], fn(x) -> rem(x,2) == 0 end) 
[1,2,3,4,5] 

我預期的[1,3,5]因爲

iex> Enum.map([0,1,2,3,4,5], fn(x) -> rem(x,2) == 0 end) 
[true,false,true,false,true,false] 

我想了解它是如何工作的,而不是試圖得到我想要的輸出的輸出(有Enum.filter實現)的結果

回答

3

您正在尋找Enum.reject/2,而不是Enum.drop_while/2。如文件所述,Enum.drop_while開始開始下降,直到fun返回真值。在你的例子中,fun返回true1,所以你得到原始列表的所有元素,從1開始。

iex(1)> Enum.reject([0, 1, 2, 3, 4, 5], fn(x) -> rem(x, 2) == 0 end) 
[1, 3, 5] 
+0

所以,'直到一個真值'是理解它的關鍵。 – Bala