Elixir: Enum函數總結

elixr Enum函數總結

all?(t, (element -> as_boolean(term))) :: boolean

all把列表中的每一個元素傳遞給Enum.all的第二個匿名函數參數, 當該函數對於列表中的全部值都返回true時, Enum.all?返回true, 不然返回false 例如python

iex>Enum.all?([2, 4, 6], fn(x) -> rem(x, 2) == 0 end)
true

iex> Enum.all?([2, 3, 4], fn(x) -> rem(x, 2) == 0 end)
false

若是未定義匿名函數fn 則檢查列表中的全部元素是否爲true例如(在Elixir中, 只有falsenil被認爲是false, 其餘任意值均被認爲是true )c++

iex> Enum.all?([1, 2, 3])
true

iex> Enum.all?([1, nil, 3])
false

iex> Enum.all?([1, 0, 3])
true

any函數

any?(t, (element -> as_boolean(term))) :: boolean

函數anyall相似, any判斷列表中的單個元素是否知足fn函數的,只要有一個知足fn函數則返回值爲true不然爲 false
例:算法

iex> Enum.any?([2, 4, 6], fn(x) -> rem(x, 2) == 1 end)
false

iex> Enum.any?([2, 3, 4], fn(x) -> rem(x, 2) == 1 end)
true

若是未定義fn, 則檢查列表中的全部元素是否存在爲true的值,只要有一個true則返回true不然爲false
例:數組

iex> Enum.any?([false, false, false])
false

iex> Enum.any?([false, true, false])
true

iex> Enum.any?([false, 1, false])
true

at函數

at(t, integer, default) :: element | default

at函數查看列表中的元素,第二個參數integer則表示列表中的第幾個元素至關於c++,python中的數組下表例如函數

list= [2,4,6]
list[0] = 2
list[1] = 4
list[2] = 6
list[-1] = 6
list[-2] = 4
list[-3] = 2
iex> Enum.at([2, 4, 6], 0)
2

iex> Enum.at([2, 4, 6], 1)
4

iex> Enum.at([2, 4, 6], 2)
6

iex> Enum.at([2, 4, 6], -1)
6

iex> Enum.at([2, 4, 6], -2)
4

iex> Enum.at([2, 4, 6], -3)
2

超出元組下表的範圍則返回nilfetch

iex> Enum.at([2, 4, 6], -4)
nil
iex> Enum.at([2, 4, 6], 3)
nil

該函數可設置默認超表返回值例如code

iex> Enum.at([2, 4, 6], 4, :none)
:none
iex> Enum.at([2, 4, 6], 4, :error)
:error

chunk函數

chunk(t, pos_integer, pos_integer, t | nil) :: [list]

chunk函數第一個參數爲列表,第二個參數爲分隔列表的元素項數, 第三個爲可選參數起始分隔的偏移項數,最後一個>參數爲補位
簡單的說第二個參數即 參與分隔的項目例如three

iex> Enum.chunk([1, 2, 3, 4, 5, 6], 2)
[[1, 2], [3, 4], [5, 6]]
# 2表示沒兩項分隔一次

iex> Enum.chunk([1, 2, 3, 4, 5, 6], 3)
[[1,2,3],[4,5,6]]
# 每三項分隔一次

假設咱們沒四項分隔一次element

iex> Enum.chunk([1, 2, 3, 4, 5, 6], 4)
[[1,2,3,4]]

你會發現分隔沒法徹底,由於[5,6]還缺兩項才能構成一個完整分隔,因此這裏只分隔出了一項即[1,2,3,4]
如今開始介紹第二個參數
第二個參數爲分隔初始元素偏移量,例如:rem

iex> Enum.chunk([1, 2, 3, 4, 5, 6], 4, 1)
[[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6]]

你會發現第一組分隔完成第二組分隔在第一組分的頭元素偏移了一組,也就是從2開始再次分隔出一組數據,如今咱們將偏移量改成2和3(偏移2個元素和3個元素)

iex> Enum.chunk([1, 2, 3, 4, 5, 6], 4, 2)
[[1, 2, 3, 4], [3, 4, 5, 6]]

iex> Enum.chunk([1, 2, 3, 4, 5, 6], 4, 3)
[[1, 2, 3, 4]]

當偏移量改成3時,第二組分隔組將從4開始[4,5,6]少一個元素才能構成完成的分隔項,這時候咱們引入第三個參數補位列表例如:

iex> Enum.chunk([1, 2, 3, 4, 5, 6], 4, 3, [7])
[[1, 2, 3, 4], [4, 5, 6, 7]]

iex> Enum.chunk([1, 2, 3, 4, 5, 6], 5, 3,[7, 8])
[[1, 2, 3, 4, 5], [4, 5, 6, 7, 8]]

當Enum.chunk([1, 2, 3, 4, 5, 6], 4, 3) 第二個完整分隔缺乏一個數的時候,咱們用補位參數[7]填充
當Enum.chunk([1, 2, 3, 4, 5, 6], 5, 3) 第二個完整分隔缺乏兩個數的時候,咱們用補位參數[7,8]來填充
注意: 不管是分隔參數仍是補位參數 都不要求連續 而且補位參數並不限定個數

iex> Enum.chunk([1, 3, 2, 40, 51, 16], 5, 3,[27, 18])
[[1, 3, 2, 40, 51], [40, 51, 16, 27, 18]]

iex> Enum.chunk([1, 3, 2, 40, 51, 16], 5, 3,[27, 18, 20, 30, 40])
[[1, 3, 2, 40, 51], [40, 51, 16, 27, 18]]

chunk_by函數

chunk_by(t, (element -> any)) :: [list]

chunk_by判斷分割函數,第一個參數爲列表,第二個參數爲判斷語句,當判斷結果爲true時該元素被分割出來(如連續連個元素判斷爲true則被分割在一個完成分割列表中)不然不分割,例如:

iex> Enum.chunk_by([1, 2, 3, 4, 4, 6, 7, 7], fn(x) -> x <= 3 end)
[[1, 2, 3], [4, 4, 6, 7, 7]]

iex> Enum.chunk_by([1, 2, 3, 4, 4, 6, 7, 7], fn(x) -> x <= 4 end)
[[1, 2, 3,4,4], [6, 7, 7]]

iex> Enum.chunk_by([1, 2, 2, 3, 4, 4, 6, 7, 7], &(rem(&1, 2) == 1))
[[1], [2, 2], [3], [4, 4, 6], [7, 7]]

iex> Enum.chunk_by([1, -1, 3, 4, -7], &(&1) < 0)
[[1], [-1], [3, 4], [-7]]

concat函數

concat(t, t) :: t

連接函數,將列表中的元素連接起來,有點相似erlang中的扁平化函數,例如:

iex> Enum.concat([1..3, 4..6, 7..9])
[1, 2, 3, 4, 5, 6, 7, 8, 9]

iex> Enum.concat([[1, [2], 3], [4], [5, 6]])
[1, [2], 3, 4, 5, 6]

count函數

count(t, (element -> as_boolean(term))) :: non_neg_integer

長度函數,計算長度例如

iex> Enum.count([1, 2, 3])
3

函數第二個參數爲可選參數,可選參數是判斷語句,計算出知足語句的元素個數例如:

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

iex> Enum.count([-1, -2, -3, 4, 5], &(&1) < 0)
3

dedup函數

dedup(t) :: list

dedup函數爲去除重複項函數,該函數將對list中===運算結果爲true的項進行去重, 例如

iex> Enum.dedup([1, 2, 3, 3, 2, 1])
[1, 2, 3, 2, 1]

iex> Enum.dedup([1, 1, 2, 2.0, :three, :"three"])
[1, 2, 2.0, :three]

dedup_by函數

dedup_by(t, (element -> term)) :: list

dedup_by函數將會對list中的元素進行條件去重,該函數的第二個參數爲算法,用該算法,將list中的每個元素一次運算,而後獲得的運算結果參與去重運算,去重運算結束後,被去掉的結果項對應的原項將被刪去.
例如:

iex> Enum.dedup_by([{1, :a}, {2, :b}, {2, :c}, {1, :a}], fn {x, _} -> x end)
[{1, :a}, {2, :b}, {1, :a}]

iex> Enum.dedup_by([5, 1, 2, 3, 2, 1], fn x -> x > 2 end)
[5, 1, 3, 2]

drop函數

drop(t, integer) :: list

截去函數,(相似python中的list[1:]的用法),當integer爲正數n時,表示去掉前n項,當'integer'負數時表示去掉後n的絕對值項,(注意,列表項數從1開始計數)例如:

iex> Enum.drop([1, 2, 3], 2)
[3]

iex> Enum.drop([1, 2, 3], 10)
[]

iex> Enum.drop([1, 2, 3], 0)
[1, 2, 3]

iex> Enum.drop([1, 2, 3], -1)
[1, 2]

drop_while函數

drop_while(t, (element -> as_boolean(term))) :: list

條件截取函數,按照第二個參數給與的條件,進行條件篩選,知足條件的項將會被刪去例如:

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

each函數

each(t, (element -> any)) :: :ok

依次執行函數,將會對list中的元素依次執行參數二中的算法,切記全部元素執行結束後,該函數的返回結果真是一個原子:ok,例如:

Enum.each(["some", "example"], fn(x) -> IO.puts x end)
"some"
"example"
:ok

iex> Enum.each([1,2,3,4], fn(x) -> x+1 end)
:ok

empty?函數

empty?(t) :: boolean

正如名字同樣,該函數肯定列表中是否爲空,空則返回true不然false

iex> Enum.empty?([])
true

iex> Enum.empty?([1, 2, 3])
false

fetch函數

fetch(t, integer) :: {:ok, element} | :error

該函數返回列表中的元素,第二個參數爲整數,能夠看作是c++中的數組下表,從0開始計數例,若是存在則返回{:ok, 元素},不存在則返回:error如:

iex> Enum.fetch([2, 4, 6], 0)
{:ok, 2}

iex> Enum.fetch([2, 4, 6], 2)
{:ok, 6}

iex> Enum.fetch([2, 4, 6], 4)
:error

fetch!函數

fetch!(t, integer) :: element | no_return

該函數與fetch函數基本相似,區別在與返回值形式不同fetch!函數直接返回元素或拋出錯誤例如:

iex> Enum.fetch!([2, 4, 6], 0)
2

iex> Enum.fetch!([2, 4, 6], 2)
6

iex> Enum.fetch!([2, 4, 6], 4)
** (Enum.OutOfBoundsError) out of bounds error

filter函數

filter(t, (element -> as_boolean(term))) :: list

filter函數的第一個參數爲基礎列表,第二個參數爲判斷語句,判讀語句逐個篩選列表中的元素,若篩選結果爲true則加入到返回值列表中例如:

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

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

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

filter_map函數

filter_map(t, (element -> as_boolean(term)), (element -> element)) :: list

filter_map函數是filter函數的晉級,該函數對基礎列表中的元素判斷,知足條件後,進一步對知足條件的元素執行新的fun,該函數的第一個參數爲基礎列表,第二個參數爲篩選方式,第三個參數爲最後執行的fun,例如

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

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

find函數

find(t, default, (element -> any)) ::element | default

find函數的第一個參數爲基礎list,第二個參數爲(可選參數)默認返回值,第三個參數爲方法funfun遍歷list中的每個參數,當fun運算至第一次爲true時返回此時對應的list中的元素,若遍歷全部元素都沒有true,切沒有可選參數對應的默認值則返回nil,如有默認參數,則返回默認參數,例如:

iex> Enum.find([2, 4, 6], fn(x) -> rem(x, 2) == 1 end)
nil

iex> Enum.find([2, 4, 6], 0, fn(x) -> rem(x, 2) == 1 end)
0

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

iex> Enum.find([2, 3, 6, 5], 10, fn(x) -> rem(x, 2) == 1 end)
3

find_index函數

find_index(t, (element -> any)) :: index | nil

函數find_index和函數find基本同樣,只是返回結果上,find返回的是列表中的元素,而find_index返回的是元素在列表中的序號(c++中數組的下表),且find_index函數沒有可選參數作的默認值選項,例如:

iex> Enum.find_index([2, 4, 6], fn(x) -> rem(x, 2) == 1 end)
nil

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

iex> Enum.find_index([2, 4, 3, 6], fn(x) -> rem(x, 2) == 1 end)
2

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

find_value函數

find_value(t, any, (element -> any)) :: any | nil

該函數與find函數相似,只是在知足fun表達式的返回結果上,find函數返回的是列表中的元素,而該函數返回的則是true,例如

iex> Enum.find_value([2, 4, 6], fn(x) -> rem(x, 2) == 1 end)
nil

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

iex> Enum.find_value([1, 2, 3], "no bools!", &is_boolean/1)
"no bools!"

未完待續

歡迎斧正

相關文章
相關標籤/搜索