ruby reduce方法
Posted miaomiaotab
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby reduce方法相关的知识,希望对你有一定的参考价值。
#####inject
inject
是我使用最频繁的方法了,它的强大之处在于可以方便的对嵌套的数组,哈希等混合数据结构进行合并或求和, 可以有效减少代码量。
例如最常见的数组套哈希:
1 2 3 4 |
array = [{a:100}, {b:200}, {c:300}]
array.inject(0) { |sum, e| sum += e.values.first } #600
array.inject({}) { |sum, e| sum.merge e } #{:a=>100, :b=>200, :c=>300}
|
- inject():括号中的是sum的初始值
- sum, e: 和在前,数组元素在后,中间必须以逗号隔开
#####group_by
group_by
适用于对于数组和hash的分组. 在stackoverflow
,我经常遇到这样的问题:
array
根据相同的school_id
进行分组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
array = [{"school_id"=>"1",
"plan_type"=>"All",
"view"=>"true",
"create"=>"true",
"approve"=>"true",
"grant"=>"true",
"region_id"=>nil},
{"school_id"=>"1",
"plan_type"=>"All",
"edit"=>"true",
"region_id"=>nil},
{"school_id"=>"2",
"plan_type"=>"All",
"edit"=>"true",
"grant"=>"true",
"region_id"=>nil}]
array.group_by { |e| e["school_id"] }
=> {"1"=>[{"school_id"=>"1", "plan_type"=>"All", "view"=>"true", "create"=>"true", "approve"=>"true", "grant"=>"true", "region_id"=>nil}, {"school_id"=>"1", "plan_type"=>"All", "edit"=>"true", "region_id"=>nil}], "2"=>[{"school_id"=>"2", "plan_type"=>"All", "edit"=>"true", "grant"=>"true", "region_id"=>nil}]}
|
多条件分组:
1 2 |
array.group_by { |e| [e["school_id"], e["plan_type"]] } #将多个条件放在数组当中
=> {["1", "All"]=>[{"school_id"=>"1", "plan_type"=>"All", "view"=>"true", "create"=>"true", "approve"=>"true", "grant"=>"true", "region_id"=>nil}, {"school_id"=>"1", "plan_type"=>"All", "edit"=>"true", "region_id"=>nil}], ["2", "All"]=>[{"school_id"=>"2", "plan_type"=>"All", "edit"=>"true", "grant"=>"true", "region_id"=>nil}]}
|
####reduce
reduce
作用和inject
优点类似,但是它比inject
还要简洁
1 2 3 |
(5..10).inject {|sum, n| sum + n } # 45
(5..10).reduce(:+) # 45
(5..10).reduce(1, :+) # 46 (括号中第一个参数是初始值,第二个是方法名)
|
以group_by
中的array
为例,将相同school_id
的hash进行合并
1 2 |
array.group_by { |e| e["school_id"] }.values.map { |i| i.inject({}) { |sum ,e| sum.merge e }}
=> [{"school_id"=>"1", "plan_type"=>"All", "view"=>"true", "create"=>"true", "approve"=>"true", "grant"=>"true", "region_id"=>nil, "edit"=>"true"}, {"school_id"=>"2", "plan_type"=>"All", "edit"=>"true", "grant"=>"true", "region_id"=>nil}]
|
可以使用inject
将hash合并,但是使用reduce效果会更好
1 2 |
array.group_by { |e| e["school_id"] }.values.map { |i| i.reduce(:merge) }
=> [{"school_id"=>"1", "plan_type"=>"All", "view"=>"true", "create"=>"true", "approve"=>"true", "grant"=>"true", "region_id"=>nil, "edit"=>"true"}, {"school_id"=>"2", "plan_type"=>"All", "edit"=>"true", "grant"=>"true", "region_id"=>nil}]
|
####zip
zip
可以将两个数组合并为一个二维数组
1 2 3 4 |
a= [1,2,3,4,5]
b=[6,7,8,9,10]
a.zip(b)
=> [[1, 6], [2, 7], [3, 8], [4, 9], [5, 10]]
|
如果a.length > b.length
,b中缺少的以nil代替 ,
1 2 3 4 |
a=[1,2,3,4,5,6]
b=[6,7,8,9,10]
a.zip(b)
=> [[1, 6], [2, 7], [3, 8], [4, 9], [5, 10], [6, nil]]
|
如果a.length < b.length
,b中多余的直接被丢弃
1 2 3 4 |
a= [1,2,3,4]
b=[6,7,8,9,10]
a.zip(b)
=> [[1, 6], [2, 7], [3, 8], [4, 9]]
|
以上是关于ruby reduce方法的主要内容,如果未能解决你的问题,请参考以下文章