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)
trueiex> Enum.all?([2, 3, 4], fn(x) -> rem(x, 2) == 0 end)
false
如果未定义匿名函数fn
则检查列表中的所有元素是否为true
例如(在Elixir中, 只有false
和nil
被认为是false
, 其他任意值均被认为是true
)
iex> Enum.all?([1, 2, 3])
trueiex> Enum.all?([1, nil, 3])
falseiex> 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)
falseiex> Enum.any?([2, 3, 4], fn(x) -> rem(x, 2) == 1 end)
true
如果未定义fn
, 则检查列表中的所有元素是否存在为true
的值,只要有一个true
则返回true
否则为false
例:
iex> Enum.any?([false, false, false])
falseiex> Enum.any?([false, true, false])
trueiex> 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)
2iex> Enum.at([2, 4, 6], 1)
4iex> Enum.at([2, 4, 6], 2)
6iex> Enum.at([2, 4, 6], -1)
6iex> Enum.at([2, 4, 6], -2)
4iex> 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)
2iex> 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"
:okiex> Enum.each([1,2,3,4], fn(x) -> x+1 end)
:ok
empty?
函数
empty?(t) :: boolean
正如名字一样,该函数确定列表中是否为空,空则返回true
否则false
iex> Enum.empty?([])
trueiex> 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)
2iex> Enum.fetch!([2, 4, 6], 2)
6iex> 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)
niliex> Enum.find([2, 4, 6], 0, fn(x) -> rem(x, 2) == 1 end)
0iex> Enum.find([2, 3, 4], fn(x) -> rem(x, 2) == 1 end)
3iex> 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)
niliex> Enum.find_index([2, 3, 4], fn(x) -> rem(x, 2) == 1 end)
1iex> Enum.find_index([2, 4, 3, 6], fn(x) -> rem(x, 2) == 1 end)
2iex> 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)
niliex> Enum.find_value([2, 3, 4], fn(x) -> rem(x, 2) == 1 end)
trueiex> Enum.find_value([1, 2, 3], "no bools!", &is_boolean/1)
"no bools!"
未完待续
欢迎斧正
Elixir: Enum函数总结相关推荐
- Elixir 与 Go 对比 [翻译]
阅读时间: 16 分钟 之前一直做 Python 和 Rails(暂时代替 Ruby) 的开发. 这两种语言都有自己的优势,应用都很广泛,但也不乏有缺陷,最突出的问题莫过于性能了.所以,在高并发到来之 ...
- c大小写转换函数_字符处理——大小写转换编程思路扩展
1.简单实现 前面我们实现了首字母大写.转换大写.转换小写3个功能,如果再实现一个大小写互换,有了前面的基础,实现起来应该没什么大问题: Function FUpperLowerSwap(str As ...
- 《Elixir In Action》第一章--第五章笔记
目录 一.iex 文件名 二. 关于返回值 三. 库的载入 1. import 2. alias 库名, as: 你喜欢的名字 3. alias 库名 四. @符号 1. 定义模块内常量 2. 编写文 ...
- elixir 教程_认识Elixir,Laravel编译资产的方式
elixir 教程 Many thanks to Jad Joubran, Gabriel Zerbib, Anthony Chambers, and Scott Molinari for peer ...
- python3 枚举定义和使用
定义 在某些情况下,一个类的对象是有限且固定的,比如季节类,它只有 4 个对象:再比如行星类,目前只有 8 个对象.这种实例有限且固定的类,在 Python 中被称为枚举类. 程序有两种方式来定义枚举 ...
- Python枚举类型的使用
枚举类型是在Python3.4新增到Python的标准库. 创建枚举 Python提供了两种方法来创建枚举: 基于class语法创建枚举 基于Function API创建枚举 创建枚举首先要导入Enu ...
- iOS Coding Style Guide 代码规范
前言 代码规范可以说是老生常谈的话题了, 也是程序员自我修养的一种体现, 虽然一套好的代码规范不能使程序运行的更加流畅, 不能使程序直接的影响到程序的功能执行,但是如果能再发开之前就能明确定义一套代码 ...
- 集算器协助SQL实现非等值分组
SQL通常只能按源表字段进行分组,如果分组依据来自另一张表.外部参数.条件列表,用SQL就很繁琐.有时分组依据需要动态生成,这些往往要借助高级语言实现.有时分组依据和源表不完全对应(或区间没有交集), ...
- VB.NET 判断文本的换行符
VB.NET Code: '设置枚举 Enum NewlineCharacter Unknown=0 CR=1'早期Macintosh系统的换行符 LF=2'Unix/Linux系统的换行符 CRLF ...
最新文章
- Quality Certificate Check at Goods Receipt
- 【软件安装】IDEA2018版安装
- python调试神器_介绍一款调试Python的神器
- shell 编程 变量
- 添加dubbo xsd的支持
- 打开windows批处理大门
- 那年学过的web后端笔记
- 【值得学习】售前工程师的成长(四)---一个老员工的经验之谈
- 微服务笔记(二) 服务发现
- 【速转身边开公司的朋友】连加油站都被比特币勒索病毒黑了!还没关闭服务器445端口的抓紧!...
- android studio 安装sdk和android,请问android studio怎么安装sdk
- 百度文库免费下载方法,亲测可用!建议收藏!
- 天涯明月刀ol最新服务器列表,天涯明月刀手游开服表 新区开服时间表汇总[多图]...
- 黑龙江省牡丹江市谷歌高清卫星地图下载
- 【音视频】iOS AVAudioSession梳理
- 阿里云ECS vpc网络与办公网内网互通
- Python 检测PE所启用保护方式
- 【Command】sudo rm -rf /* 啥效果?
- Google搜索中国定制版已黄了,百度再次PK的希望或落空
- 关于屏幕分辨率的一点常识