array.c: added method that verifies if an Array is part of another #127

Closed
wants to merge 1 commit into
from

Projects

None yet

6 participants

@lellisga

This method is like the include? method but instead of receiving a value and check if the array has it, it receives an array an check if it's part of another one.

@nobu
Member
nobu commented May 30, 2012

file a Feature Request in bugs.ruby-lang.org.

@nobu
Member
nobu commented May 30, 2012

It surprised me that ["a", "c"].part_of? ["a", "b", "c"] returns true.
Should not put local variable declarations after executable code.

@lellisga

@nobu the way I see it is ["a", "b", "c"] includes ["a", "c"]. That's way it returns true.

@nobu
Member
nobu commented May 30, 2012

It seemed like returning true if X is a portion of Y to me, where X.part_of?(Y).
I expected only this would return true:

["a", "b"].part_of?(["a", "b", "c") #=> true

Anyway, you should go to http://bugs.ruby-lang.org/projects/ruby-trunk/issues/new

@lellisga

@nobu Done with the Feature Request: http://bugs.ruby-lang.org/issues/6515

@hmaddocks

1.9.3p125 :001 > a = [1,2,3,4,5]
=> [1, 2, 3, 4, 5]
1.9.3p125 :002 > b = [2,3,4]
=> [2, 3, 4]
1.9.3p125 :003 > c = [4,5,6]
=> [4, 5, 6]
1.9.3p125 :004 > b == a & b
=> true
1.9.3p125 :005 > c == a & c
=> false

@lellisga

yap @hmaddocks. You're right, that's Set Theory. I'm proposing a much easier way to implement this theory making things much faster. If you review the code you can notice that I'm not comparing the sets because it's more expensive, instead I'm comparing the difference between them with an empty array.

@sarcilav

@hmaddocks I tested this PR and one importance difference is that it isn't order dependent as your example

ruby-1.9.3-p125 :001 > a = [1,2,3,4,5]
 => [1, 2, 3, 4, 5] 
ruby-1.9.3-p125 :002 > b = [4,3,2]  # it is your b but in a reverse order
 => [4, 3, 2] 
ruby-1.9.3-p125 :003 > b == a & b
 => false

but

ruby-1.9.3-p125 :001 > [4,3,2].part_of? [1,2,3,4,5]
=> true
@itsmeduncan itsmeduncan commented on an outdated diff May 30, 2012
@@ -4686,6 +4686,30 @@ enum {
}
/*
+ * call-seq:
+ * ary.part_of? other_ary -> bool
+ *
+ * Array 'A' is part of another array 'B' if
+ * each element from 'A' are include in 'B'
@hmaddocks

The expression

array_1.part_of? array_2

implies to me that you are treating the arrays as atomic units yet your implementation is treating the array as an arbitrary collection of objects i.e., it's the contained objects that are being tested not the array.
[2,3,4] != [4,3,2] even though they contain the same objects.
Seems to me that you want an iterative version of include?

1.9.3p125 :001 > a = [1,2,3,4,5] => [1, 2, 3, 4, 5] 1.9.3p125 :002 > b = [2,3,4] => [2, 3, 4] 1.9.3p125 :003 > a.include? b => false 1.9.3p125 :004 > b.inject{|memo, obj| memo && a.include?(obj) } => true 1.9.3p125 :005 > c = [4,3,2] => [4, 3, 2] 1.9.3p125 :006 > c.inject{|memo, obj| memo && a.include?(obj) } => true 1.9.3p125 :007 > d = [4,5,6] => [4, 5, 6] 1.9.3p125 :008 > d.inject{|memo, obj| memo && a.include?(obj) } => false

@zzak
Member
zzak commented Nov 19, 2012

I'm closing this, please refer to Feature #6515 on redmine. Thank you!

@zzak zzak closed this Nov 19, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment