當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Elixir Stream.map_every用法及代碼示例

Elixir語言中 Stream.map_every 相關用法介紹如下。

用法:

map_every(enum, nth, fun)
(從 1.4.0 開始)
@spec map_every(Enumerable.t(), non_neg_integer(), (element() -> any())) ::
  Enumerable.t()

創建一個流,該流將在可枚舉的每個 nth 元素上應用給定函數。

第一個元素總是傳遞給給定的函數。

nth 必須是非負整數。

例子

iex> stream = Stream.map_every(1..10, 2, fn x -> x * 2 end)
iex> Enum.to_list(stream)
[2, 2, 6, 4, 10, 6, 14, 8, 18, 10]

iex> stream = Stream.map_every([1, 2, 3, 4, 5], 1, fn x -> x * 2 end)
iex> Enum.to_list(stream)
[2, 4, 6, 8, 10]

iex> stream = Stream.map_every(1..5, 0, fn x -> x * 2 end)
iex> Enum.to_list(stream)
[1, 2, 3, 4, 5]

相關用法


注:本文由純淨天空篩選整理自elixir-lang.org大神的英文原創作品 Stream.map_every(enum, nth, fun)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。