Arr subtract

Hi!

a=[1,1,2,3]

a - '1' # == [2,3]

I would like to get [1,2,3]

# deleting only the given (one) element - Is there a simple way to do that?

a = [1,1,1,2,2,3]
a-[1,2,3] # => [1,1,2]

thank you
Opti

Try:

a.uniq

Hani

···

On Aug 11, 2021, at 10:09 AM, Die Optimisten <inform@die-optimisten.net> wrote:

Hi!

a=[1,1,2,3]

a - '1' # == [2,3]

I would like to get [1,2,3]

# deleting only the given (one) element - Is there a simple way to do that?

a = [1,1,1,2,2,3]
a-[1,2,3] # => [1,1,2]

thank you
Opti

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Hi!

I will combine `Array#index`, `Array#delete_at` and `Enumerable#reduce` to achieve this.

The following is my answer, which may be not so simple as we expect.

arr = [1, 1, 1, 2, 2, 3]

delete_one = proc { |a, v| (idx = a.index(v)) && a.delete_at(idx); a }
delete_one[arr.dup, 1] # => [1, 1, 2, 2, 3]

brr = [1, 2, 3]
expected = [1, 1, 2]

result = brr.reduce(arr.dup, &delete_one) # => [1, 1, 2]
result == expected # => true

# Another method that mutates brr
result = arr.dup.delete_if { |v| (idx = brr.index(v)) ? (brr.delete_at(idx) && true) : false} # => [1, 1, 2]
result == expected # => true
···

-----Original Messages-----
From: "Die Optimisten" <inform@die-optimisten.net>
Sent Time: 2021-08-11 15:09:04 (Wednesday)
To: Ruby-Talk <ruby-talk@ruby-lang.org>
Cc:
Subject: Arr subtract

Hi!

a=[1,1,2,3]

a - '1' # == [2,3]

I would like to get [1,2,3]

# deleting only the given (one) element - Is there a simple way to do that?

a = [1,1,1,2,2,3]
a-[1,2,3] # => [1,1,2]

thank you
Opti

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

There are ways, for example:

Cheers

···

On Wed, Aug 11, 2021 at 5:10 PM Die Optimisten <inform@die-optimisten.net> wrote:

Hi!

a=[1,1,2,3]

a - '1' # == [2,3]

I would like to get [1,2,3]

# deleting only the given (one) element - Is there a simple way to do that?

a = [1,1,1,2,2,3]
a-[1,2,3] # => [1,1,2]

thank you
Opti

--
  Matthew Kerwin
  https://matthew.kerwin.net.au/

Not very performant but still:

a = [1, 1, 2, 1, 1, 2, 3]
[1, 2, 1].each_with_object(a.clone) do |x, res|
  res.find_index(x).tap { |ind| ind && res.delete_at(ind) }
end

# ==> [1, 1, 2, 3]
···

--------------------------
Dmitry Non

On 11 Aug 2021, at 09:54, Yunzhe <yunzhe@zju.edu.cn> wrote:

Hi!

I will combine `Array#index`, `Array#delete_at` and `Enumerable#reduce` to achieve this.

The following is my answer, which may be not so simple as we expect.

arr = [1, 1, 1, 2, 2, 3]

delete_one = proc { |a, v| (idx = a.index(v)) && a.delete_at(idx); a }
delete_one[arr.dup, 1] # => [1, 1, 2, 2, 3]

brr = [1, 2, 3]
expected = [1, 1, 2]

result = brr.reduce(arr.dup, &delete_one) # => [1, 1, 2]
result == expected # => true

# Another method that mutates brr
result = arr.dup.delete_if { |v| (idx = brr.index(v)) ? (brr.delete_at(idx) && true) : false} # => [1, 1, 2]
result == expected # => true

-----Original Messages-----
From: "Die Optimisten" <inform@die-optimisten.net>
Sent Time: 2021-08-11 15:09:04 (Wednesday)
To: Ruby-Talk <ruby-talk@ruby-lang.org>
Cc:
Subject: Arr subtract

Hi!

a=[1,1,2,3]

a - '1' # == [2,3]

I would like to get [1,2,3]

# deleting only the given (one) element - Is there a simple way to do that?

a = [1,1,1,2,2,3]
a-[1,2,3] # => [1,1,2]

thank you
Opti

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

P.S. don't use #tap. I used it only bc i was writing it in IRB in one line

···

--------------------------
Dmitry Non

On 11 Aug 2021, at 12:00, Dmitriy Non <non.dmitriy@gmail.com> wrote:

Not very performant but still:

a = [1, 1, 2, 1, 1, 2, 3]
[1, 2, 1].each_with_object(a.clone) do |x, res|
 res.find_index(x).tap { |ind| ind && res.delete_at(ind) }
end

# ==> [1, 1, 2, 3]

--------------------------
Dmitry Non
https://nondv.wtf
https://weird-programming.dev

On 11 Aug 2021, at 09:54, Yunzhe <yunzhe@zju.edu.cn> wrote:

Hi!

I will combine `Array#index`, `Array#delete_at` and `Enumerable#reduce` to achieve this.

The following is my answer, which may be not so simple as we expect.

arr = [1, 1, 1, 2, 2, 3]

delete_one = proc { |a, v| (idx = a.index(v)) && a.delete_at(idx); a }
delete_one[arr.dup, 1] # => [1, 1, 2, 2, 3]

brr = [1, 2, 3]
expected = [1, 1, 2]

result = brr.reduce(arr.dup, &delete_one) # => [1, 1, 2]
result == expected # => true

# Another method that mutates brr
result = arr.dup.delete_if { |v| (idx = brr.index(v)) ? (brr.delete_at(idx) && true) : false} # => [1, 1, 2]
result == expected # => true

-----Original Messages-----
From: "Die Optimisten" <inform@die-optimisten.net>
Sent Time: 2021-08-11 15:09:04 (Wednesday)
To: Ruby-Talk <ruby-talk@ruby-lang.org>
Cc:
Subject: Arr subtract

Hi!

a=[1,1,2,3]

a - '1' # == [2,3]

I would like to get [1,2,3]

# deleting only the given (one) element - Is there a simple way to do that?

a = [1,1,1,2,2,3]
a-[1,2,3] # => [1,1,2]

thank you
Opti

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

a=[1,1,2,3]
a - '1' # == [2,3]

a-[1]

I would like to get [1,2,3]
# deleting only the given (one) element - Is there a simple way to do that?

a = [1,1,1,2,2,3]
a-[1,2,3] # => [1,1,2]

not simpler, so try harder.
eg,

b=[1,2,1]

=> [1, 2, 1]

a=[1,1,2,1,1,2,3]

=> [1, 1, 2, 1, 1, 2, 3]

b.each.with_object(a){|b_element,a_obj| i=a_obj.index(b_element);

a_obj.delete_at(i) if i }
=> [1, 1, 2, 3]

hth.
--botp

···

On Wed, Aug 11, 2021 at 3:09 PM Die Optimisten <inform@die-optimisten.net> wrote:

a = [1,1,1,2,2,3]
=> [1, 1, 1, 2, 2, 3]
remove = [1,2,3]
=> [1, 2, 3]
remove.each{|r| a.delete_at(a.index(r))}
=> [1, 2, 3]
a
=> [1, 1, 2]