Skip to content

Instantly share code, notes, and snippets.

@tuscen
Last active August 29, 2015 14:13
Show Gist options
  • Save tuscen/6711d3baf77d1bdc6d08 to your computer and use it in GitHub Desktop.
Save tuscen/6711d3baf77d1bdc6d08 to your computer and use it in GitHub Desktop.
def conjunctionSelect obj,arg,*args
res = []
for i in obj
res << i if ([arg]+args).inject do |acc,n|
n.call(i) && acc.call(i)
end
#res << i if ([arg]+args).all? {|n| n.call(i)}
end
res
end
class Array
def all?
if block_given? then
self.inject do |acc,i|
yield(acc) && yield(i)
end
else
self.inject do |acc,i|
acc && i
end
end
end
def any? &b
unless b then
self.inject do |acc,i|
acc || i
end
else
self.inject do |acc,i|
b.call(acc) || b.call(i)
end
end
end
end
p [-1,9,1].any?{|i| i>0}
p [1,2,3,-6].all?{|i|i<0}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment