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中, 只有false
和nil
被认为是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
函数any
与all
相似, 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
超出元组下表的范围则返回nil
fetch
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,第二个参数为(可选参数)默认返回值,第三个参数为方法fun
,fun
遍历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!"