只有当对象包含在另一个数组中时,我才想删除数组中的对象。
myArray = myArray - otherArray
如何将此行为添加为 CollectionType 的扩展
最佳答案
因此,您可以通过多种方式来实现。我已经要求这些版本中的元素是可散列的,但是对于它们来说,方法将是相似的(如果慢得多的话)只是为了等同。
如果您的集合符合 RangeReplaceableCollectionType,则它可以使用 removeAtIndex 方法。这意味着您可以返回与给定类型相同类型的集合:
extension RangeReplaceableCollectionType where Generator.Element : Hashable {
mutating func subtractInPlace(vals: Set<Generator.Element>) {
// The indices need to be reversed, because removing at a given index invalidates all
// those above it
for idx in indices.reverse()
where vals.contains(self[idx]) { // This is why hashable is a requirement: the
removeAtIndex(idx) // contains method is much more efficient on sets
}
}
mutating func subtractInPlace<
S : SequenceType where
S.Generator.Element == Generator.Element
>(seq: S) {
subtractInPlace(Set(seq))
}
func subtract(vals: Set<Generator.Element>) -> Self {
var col = self
col.subtractInPlace(vals)
return col
}
func subtract<S : SequenceType where S.Generator.Element == Generator.Element>(seq: S) -> Self {
return subtract(Set(seq))
}
}
否则,您将只返回一个数组。 (其实我觉得这个方法更快)
extension SequenceType where Generator.Element : Hashable {
func subtract(vals: Set<Generator.Element>) -> [Generator.Element] {
return filter { !vals.contains($0) }
}
func subtract<
S : SequenceType where
S.Generator.Element == Generator.Element
>(seq: S) -> [Generator.Element] {
return subtract(Set(seq))
}
}
然后,您需要定义运算符。这里有这么多不同版本的原因是 Swift 会在每种情况下选择最具体的实现。因此,在转换为集合的版本之前,将选择带有集合的版本。这使您能够实现高效的实现,而不会使其他效率较低的实现无效。
func - <
C : RangeReplaceableCollectionType where
C.Generator.Element : Hashable
>(lhs: C, rhs: Set<C.Generator.Element>) -> C {
return lhs.subtract(rhs)
}
func - <
C : RangeReplaceableCollectionType,
S : SequenceType, T : Hashable where
C.Generator.Element == T,
S.Generator.Element == T
>(lhs: C, rhs: S) -> C {
return lhs.subtract(rhs)
}
func - <
S : SequenceType where
S.Generator.Element : Hashable
>(lhs: S, rhs: Set<S.Generator.Element>) -> [S.Generator.Element] {
return lhs.subtract(rhs)
}
func - <
S0 : SequenceType,
S1 : SequenceType,
T : Hashable where
S0.Generator.Element == T,
S1.Generator.Element == T
>(lhs: S0, rhs: S1) -> [T] {
return lhs.subtract(rhs)
}
关于arrays - Swift "-"CollectionTypes 上的二元运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31543956/
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我怎样才能完成http://php.net/manual/en/function.call-user-func-array.php在ruby中?所以我可以这样做:classAppdeffoo(a,b)putsa+benddefbarargs=[1,2]App.send(:foo,args)#doesn'tworkApp.send(:foo,args[0],args[1])#doeswork,butdoesnotscaleendend 最佳答案 尝试分解数组App.send(:foo,*args)
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
通过rubykoans.com,我在about_array_assignment.rb中遇到了这两段代码你怎么知道第一个是非并行赋值,第二个是一个变量的并行赋值?在我看来,除了命名差异之外,代码几乎完全相同。4deftest_non_parallel_assignment5names=["John","Smith"]6assert_equal["John","Smith"],names7end45deftest_parallel_assignment_with_one_variable46first_name,=["John","Smith"]47assert_equal'John
我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问
请帮助我理解范围运算符...和..之间的区别,作为Ruby中使用的“触发器”。这是PragmaticProgrammersguidetoRuby中的一个示例:a=(11..20).collect{|i|(i%4==0)..(i%3==0)?i:nil}返回:[nil,12,nil,nil,nil,16,17,18,nil,20]还有:a=(11..20).collect{|i|(i%4==0)...(i%3==0)?i:nil}返回:[nil,12,13,14,15,16,17,18,nil,20] 最佳答案 触发器(又名f/f)是
我将我的Rails应用程序部署到OpenShift,它运行良好,但我无法在生产服务器上运行“Rails控制台”。它给了我这个错误。我该如何解决这个问题?我尝试更新rubygems,但它也给出了权限被拒绝的错误,我也无法做到。railsc错误:Warning:You'reusingRubygems1.8.24withSpring.UpgradetoatleastRubygems2.1.0andrun`gempristine--all`forbetterstartupperformance./opt/rh/ruby193/root/usr/share/rubygems/rubygems
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que