用map, next 和 reject 简化代码(ruby)
来源:互联网 发布:网络射击游戏那个好玩 编辑:程序博客网 时间:2024/04/29 06:11
For example, we have this Channel class:
class Channel attr_reader :id, :name def initialize(id, name) @id = id @name = name endend
1.
What we have:
One list of objects:
current_channels = [Channel.new(1, 'channel1'), Channel.new(2, 'channel2'), Channel.new(3, 'channel3')]
Task:Get arrays of ids from this list.
1.1 Bad solution
current_channel_ids = []current_channels.each do |channel| current_channel_ids << channel.idend
1.2 Better solution using map
current_channel_ids = current_channels.map { |channel| channel.id }
1.3 Best solution using map
current_channel_ids = current_channels.map(&:id)
2.
What we have:
Two lists of objects:
current_channels = [Channel.new(1, 'channel1'), Channel.new(2, 'channel2'), Channel.new(3, 'channel3')]need_to_be_assigned_channels = [ Channel.new(1, 'channel1'), Channel.new(4, 'channel4')]
Task:Assign channels in need_to_be_assigned_channels to current_channels array and make sure there're no two channels with identical id.
Solution:
Before each solution, get the an array of ids from current_channels:
current_channel_ids = current_channels.map(&:id)
2.1 Bad solution
need_to_be_assigned_channels.each do |channel| unless current_channel_ids.include?(channel.id) current_channels << channel endend
2.2 Better solution using next
need_to_be_assigned_channels.each do |channel| next if current_channel_ids.include?(channel.id) current_channels << channelend
2.3 Best solution using reject
current_channels.concat(need_to_be_assigned_channels.reject { |assigned_channel| current_channel_ids.include?(assigned_channel.id) }
3.
What we have:
Two lists of objects:
current_channels = [Channel.new(1, 'channel1'), Channel.new(2, 'channel2'), Channel.new(3, 'channel3')]need_to_be_deleted_channels = [ Channel.new(1, 'channel1'), Channel.new(3, 'channel3')]
Task:Delete channels from current_channels if the id matches one of the ids in need_to_be_deleted_channels.
Solution:
Before each solution, get the an array of ids from current_channels:
current_channel_ids = current_channels.map(&:id)
3.1 Bad solution
current_channels.each do |channel| next unless need_to_be_deleted_channel_ids.include?(channel.id) current_channels.delete(channel)end
3.2 Best solution using delete_if
current_channels.delete_if { |channel| need_to_be_deleted_channel_ids.include?(channel.id) }
0 0
- 用map, next 和 reject 简化代码(ruby)
- Ruby中的map, reduce, select, reject, group_by理解
- Ruby中的map, reduce, select, reject, group_by理解
- Ruby中map,reduce,reject,select,collect,each使用介绍
- ruby , each,map,collect,inject,reject,select,shuffle[n..m],flatten
- ruby , each,map,collect,inject,reject,select,shuffle[n..m],flatten
- 用代码说话: 比较ruby和scala的集合操作each map filter reduce
- Ruby:用block简化编程一例
- QDialog的accept()函数和reject()函数
- 用Kotlin简化代码
- 用lambda简化代码
- 在Android用ZXing.jar识别二维码的精简版(简化了配置和代码)
- Android通过反射和注解简化代码编写(一)
- Ruby remove nil values from array with .reject
- ruby代码块和迭代器
- 用常量表简化代码
- android studio将代码关联到git@osc(出现 Empty repository,以及push reject解决)
- 《编写可读代码的艺术》读书笔记(下)简化循环和逻辑和重新组织代码
- Hadoop的集群数据、mapreduce管理及安全机制介绍
- 关于“ORA-01653: unable to extend table SYS.AUD$ by 128 in tablespace SYSTEM”的错误
- RNN的历史(from Deep Learning RNNaissance with Dr. Juergen Schmidhuber)
- 转 错误代码2104:无法下载Silverlight应用程序。请查看Web服务器设置
- java4Android(15)字节流大文件读写,字符流读写
- 用map, next 和 reject 简化代码(ruby)
- Effective C++ 条款10
- C# 发送邮件源码
- IOS 三级联想菜单(也可以当二级)
- 深入浅出JSONP--解决ajax跨域问题
- HDU 5266 pog loves szh III
- VC处理键盘消息
- leetcode | Climbing Stairs
- 我看我自己系列——失去平静的原因