• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

rubyreduce方法

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

#####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]]
 
 
 
 

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ruby的加密方法整理(desrsa加密加签)发布时间:2022-07-14
下一篇:
ruby符号的应用发布时间:2022-07-14
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap