rubycook book:
Recipe 4.7. Making Sure a Sorted Array Stays Sorted
class SortedArray < Array
def initialize(*args,&sort_by)
@sort_by = &sort_by || Pro.new{|x,y| x <=> y}
super(*args)
sort! @sort_by
def insert(i,v)
insert_before = index(find{|x| @sort_by.call(x,y) == 1 }
super(insert_before ? insert_before : -1,v)
end
def <<(v)
insert(0,v)
end
alias push <<
alias unshift <<
["collect!", "flatten!", "[]="].each do |method_name|
module_eval %{
def #{method_name}(*args)
super
sort! &@sort_by
end
}
end
def reverse!
#Do nothing; reversing the array would disorder it.
end
end
class
SortedArray
def []=(*args)
if args.size == 3
#e.g. "a[6,3] = [1,2,3]"
start, length, value = args
slice! Range.new(start, start+length, true)
(value.respond_to? :each) ? value.each { |x| self << x } : self << value
elsif args.size == 2
index, value = args
if index.is_a? Numeric
#e.g. "a[0] = 10" (the most common form of array assignment)
delete_at(index)
self << value
elsif index.is_a? Range
#e.g. "a[0..3] = [1,2,3]"
slice! index
(value.respond_to? :each) ? value.each { |x| self << x } : self << value
else
#Not supported. Delegate to superclass; will probably give an error.
super
sort!(&sort_by)
end
else
#Not supported. Delegate to superclass; will probably give an error.
super
sort!(&sort_by)
end
end
end
分享到:
相关推荐
"cookbook-zh-CN.md"文件很可能是这本书的主要文本部分,里面详细列举了各种使用PySimpleGUI的技巧和实践案例。每个章节通常会介绍一个特定的功能或概念,并配有相应的代码示例。通过阅读和实践这些例子,开发者可以...
python-machine-learning-cookbook-preprocessing oreilly 英文 epub格式
标题“coverage-cookbook-complete-verification-academy”表明这是一本关于覆盖度(coverage)的食谱手册,隶属于Cadence Academy的官方文件。这种手册通常包含一系列经过精心设计的指导方案,旨在帮助读者理解和...
《CMake Cookbook》是关于构建、测试和打包模块化软件的专业指南,专注于现代CMake工具的使用。本书由Radovan Bast和Roberto Di Remigio合著,旨在帮助读者掌握CMake这一强大的跨平台构建系统。 CMake是一个开源的...
Programming ArcGIS with Python Cookbook - Second Edition, mobi格式
Programming ArcGIS with Python Cookbook - Second Edition,epub格式
docker run -tid -p <port>:80 apachecn0/pandas-cookbook-code-notes # 访问 http://localhost:{port} 查看文档 PYPI pip install pandas-cookbook-code-notes pandas-cookbook-code-notes # 访问 ...
Unity Game Development Cookbook - Paris Buttfield-AddisonUnity Game Development Cookbook - Paris Buttfield-Addison
Lott -- Modern Python Cookbook -- 2016 -- code.7z
Aggarwal -- Flask Framework Cookbook -- 2014 -- code.7z
"NGINX Cookbook 高性能负载平衡高级配方" NGINX Cookbook 是一本专门介绍 NGINX 高性能负载平衡的书籍,书中涵盖了 NGINX 的高级配方和使用技巧,可以帮助读者快速搭建高性能的负载平衡系统。 GeoIP 模块和数据库...
Subramanian -- Python Data Science Cookbook -- 2015 -- code.7z
Fine -- Python 2.6 Graphics Cookbook -- 2010 -- code.7z
Zaccone -- Python Parallel Programming Cookbook -- 2015 -- code.7z
Precord -- wxPython. Application Development Cookbook -- 2015 -- code.7z
Joshi -- Python Machine Learning Cookbook -- 2016