Elixir: Enum函数总结

elixr Enum函数总结

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

all把列表中的每个元素传递给Enum.all的第二个匿名函数参数, 当该函数对于列表中的所有值都返回true时, Enum.all?返回true, 否则返回false 例如

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 )

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

超出元组下表的范围则返回nil

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

该函数可设置默认超表返回值例如

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函数第一个参数为列表,第二个参数为分隔列表的元素项数, 第三个为可选参数起始分隔的偏移项数,最后一个>参数为补位
简单的说第二个参数即 参与分隔的项目例如

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]]
# 每三项分隔一次

假设我们没四项分隔一次

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

你会发现分隔无法完全,因为[5,6]还缺两项才能构成一个完整分隔,所以这里只分隔出了一项即[1,2,3,4]
现在开始介绍第二个参数
第二个参数为分隔初始元素偏移量,例如:

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!"

未完待续

欢迎斧正

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值