Brad Tilley wrote: > In Python, I can do this to arrays:
> added = [x for x in new_data if x not in old_data] > removed = [x for x in old_data if x not in new_data] > same = [x for x in new_data if x in old_data]
> I believe this is known as list comprehension in Python. How is this done in > Ruby?
> Thanks, > Brad
This question comes up from time to time. You can search the archives of comp.lang.ruby on Google Groups for "list comprehension" to find the previous threads on this topic.
> added = [x for x in new_data if x not in old_data] > removed = [x for x in old_data if x not in new_data] > same = [x for x in new_data if x in old_data]
Short answer:
added = new_data.reject {|i| old_data.include? i } removed = old_data.reject {|i| new_data.include? i } same = new_data.select {|i| old_data.include? i }
Provided ordering isn't important here, you can do the same thing with set operations.
Brad Tilley wrote: > In Python, I can do this to arrays:
> added = [x for x in new_data if x not in old_data] > removed = [x for x in old_data if x not in new_data] > same = [x for x in new_data if x in old_data]
> I believe this is known as list comprehension in Python. How is this done in > Ruby?
> Thanks, > Brad
Because Ruby's select() returns a value, not just a boolean like in Smalltalk, you can do the following:
[1,2,3].select { |x| ![2,3,4].include? x } [2,3,4].select { |x| ![1,2,3].include? x } [1,2,3].select { |x| [2,3,4].include? x }
On 2006-11-25 18:47:26 -0500, Brad Tilley <rtil...@vt.edu> said:
> In Python, I can do this to arrays:
> added = [x for x in new_data if x not in old_data] > removed = [x for x in old_data if x not in new_data] > same = [x for x in new_data if x in old_data]
> I believe this is known as list comprehension in Python. How is this done in > Ruby?
> Thanks, > Brad
I found this while web searching for the same thing recently; I can't recall where I found it. It's a cute little hack.
class Array def comprehend return self unless block_given? result = [] self.each { |i| result.push yield(i) } result.compact end end
Then:
added = new_data.comprehend { |x| x if not old_data.include? x } removed = old_data.comprehend { |x| x if not new_data.include? x } same = new_data.comprehend { |x| x if old_data.include? x }
On Mon, 27 Nov 2006, James Cunningham wrote: > On 2006-11-25 18:47:26 -0500, Brad Tilley <rtil...@vt.edu> said:
>> In Python, I can do this to arrays:
>> added = [x for x in new_data if x not in old_data] >> removed = [x for x in old_data if x not in new_data] >> same = [x for x in new_data if x in old_data]
>> I believe this is known as list comprehension in Python. How is this done >> in >> Ruby?
>> Thanks, >> Brad
> I found this while web searching for the same thing recently; I can't recall > where I found it. It's a cute little hack.
> class Array > def comprehend > return self unless block_given? > result = [] > self.each { |i| result.push yield(i) } > result.compact > end > end
> Then:
> added = new_data.comprehend { |x| x if not old_data.include? x } > removed = old_data.comprehend { |x| x if not new_data.include? x } > same = new_data.comprehend { |x| x if old_data.include? x }
I'm not getting how that's better than:
added = new_data.select {|x| not old_data.include?(x) }
>> On 2006-11-25 18:47:26 -0500, Brad Tilley <rtil...@vt.edu> said:
>>> In Python, I can do this to arrays:
>>> added = [x for x in new_data if x not in old_data] >>> removed = [x for x in old_data if x not in new_data] >>> same = [x for x in new_data if x in old_data]
>>> I believe this is known as list comprehension in Python. How is this done in >>> Ruby?
>>> Thanks, >>> Brad
>> I found this while web searching for the same thing recently; I can't >> recall where I found it. It's a cute little hack.
>> class Array >> def comprehend >> return self unless block_given? >> result = [] >> self.each { |i| result.push yield(i) } >> result.compact >> end >> end
>> Then:
>> added = new_data.comprehend { |x| x if not old_data.include? x } >> removed = old_data.comprehend { |x| x if not new_data.include? x } >> same = new_data.comprehend { |x| x if old_data.include? x }
> I'm not getting how that's better than:
> added = new_data.select {|x| not old_data.include?(x) }
> (or the reject equivalent) and so on.
> David
I should have clarified. In your example there's no difference, but the above gives a general replacement for list comprehensions.
irb(main):018:0> (1..25).to_a.comprehend { |x| x**2 if not x % 2 == 0 } => [1, 9, 25, 49, 81, 121, 169, 225, 289, 361, 441, 529, 625]
> On 2006-11-26 17:52:37 -0500, dbl...@wobblini.net said:
>> Hi --
>> On Mon, 27 Nov 2006, James Cunningham wrote:
>>> On 2006-11-25 18:47:26 -0500, Brad Tilley <rtil...@vt.edu> said:
>>>> In Python, I can do this to arrays:
>>>> added = [x for x in new_data if x not in old_data] >>>> removed = [x for x in old_data if x not in new_data] >>>> same = [x for x in new_data if x in old_data]
>>>> I believe this is known as list comprehension in Python. How is this done in >>>> Ruby?
>>>> Thanks, >>>> Brad
>>> I found this while web searching for the same thing recently; I can't >>> recall where I found it. It's a cute little hack.
>>> class Array >>> def comprehend >>> return self unless block_given? >>> result = [] >>> self.each { |i| result.push yield(i) } >>> result.compact >>> end >>> end
>>> Then:
>>> added = new_data.comprehend { |x| x if not old_data.include? x } >>> removed = old_data.comprehend { |x| x if not new_data.include? x } >>> same = new_data.comprehend { |x| x if old_data.include? x }
>> I'm not getting how that's better than:
>> added = new_data.select {|x| not old_data.include?(x) }
>> (or the reject equivalent) and so on.
>> David
> I should have clarified. In your example there's no difference, but the > above gives a general replacement for list comprehensions.
> irb(main):018:0> (1..25).to_a.comprehend { |x| x**2 if not x % 2 == 0 } > => [1, 9, 25, 49, 81, 121, 169, 225, 289, 361, 441, 529, 625]
On 11/27/06, James Cunningham <jameshcunning...@gmail.com> wrote:
> added = new_data.comprehend { |x| x if not old_data.include? x } > removed = old_data.comprehend { |x| x if not new_data.include? x } > same = new_data.comprehend { |x| x if old_data.include? x }
> On 2006-11-26 17:52:37 -0500, dbl...@wobblini.net said: >> I'm not getting how that's better than:
>> added = new_data.select {|x| not old_data.include?(x) }
>> (or the reject equivalent) and so on. > I should have clarified. In your example there's no difference, but the > above gives a general replacement for list comprehensions.
> irb(main):018:0> (1..25).to_a.comprehend { |x| x**2 if not x % 2 == 0 } > => [1, 9, 25, 49, 81, 121, 169, 225, 289, 361, 441, 529, 625]
Frankly, I am not sure I find this better than using the built in methods: