Skip to content

Commit 29f0f0a

Browse files
committed
Add Array#delete_first, Array#delete_first_each
1 parent 17138ae commit 29f0f0a

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

lib/core/facets/array/delete_first.rb

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Array
2+
# Deletes first item from +self+ that is equal to +obj+.
3+
#
4+
# Returns the deleted item, or +nil+ if no matching item is found.
5+
#
6+
# If the optional code block is given, the result of the block is returned if
7+
# the item is not found.
8+
9+
# a = [ "a", "b", "b", "b", "c" ]
10+
# a.delete_first("b") #=> "b"
11+
# a #=> ["a", "c"]
12+
# a.delete("z") #=> nil
13+
# a.delete("z") {"not found"} #=> "not found"
14+
def delete_first(el)
15+
i = index(el)
16+
if i
17+
delete_at(i)
18+
end
19+
end
20+
end
21+
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Array
2+
# For each element of `other`, deletes the _first_ element from self that is equal to that element
3+
# (using `delete_at`, not `delete`).
4+
#
5+
# Returns a new array that is a copy of the original array, with the given elements removed.
6+
#
7+
# This is similar to `Array#-` and `Array#difference`, except that instead of removing _all_
8+
# matches, it only removes as many occurrences as you actually _ask_ it to remove:
9+
#
10+
# > [1, 1, 2].delete_first_each [1]
11+
# => [1, 2]
12+
#
13+
# > [1, 1, 2].delete_first_each [1, 1]
14+
# => [2]
15+
#
16+
# > [1, 1, 2] - [1]
17+
# => [2]
18+
#
19+
# > [1, 1, 2].difference [1]
20+
# => [2]
21+
#
22+
def delete_first_each(other)
23+
dup.delete_first_each!(other)
24+
end
25+
26+
# For each element of `other`, deletes the _first_ element from self that is equal to that element
27+
# (using `delete_at`, not `delete`).
28+
#
29+
# This is the in-place version of delete_first_each.
30+
#
31+
def delete_first_each!(other)
32+
other.each do |el|
33+
delete_first(el)
34+
end
35+
self
36+
end
37+
end
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
covers 'facets/array/delete_first_each'
2+
3+
test_case Array do
4+
method :delete_first_each do
5+
test do
6+
a = [1,1,2,3]
7+
a.delete_first_each( [1]).assert == [ 1,2,3]
8+
a.difference([1]).assert == [ 2,3]
9+
(a - [1]).assert == [ 2,3]
10+
a.assert == [1,1,2,3]
11+
end
12+
13+
test do
14+
a = [1,1,2,3]
15+
a.delete_first_each([1, 1]).assert == [ 2,3]
16+
(a - [1]).assert == [ 2,3]
17+
a.assert == [1,1,2,3]
18+
end
19+
20+
test 'example from Array#- docs' do
21+
a = [ 1, 1, 2, 2, 3, 3, 4, 5 ]
22+
(a - [ 1, 2, 4 ]).assert == [ 3, 3, 5 ]
23+
a.delete_first_each([1, 2, 4 ]).assert == [ 1, 2, 3, 3, 5 ]
24+
end
25+
26+
test 'element that is not found' do
27+
a = [1,1,2,3]
28+
(a - [4]).assert == [1,1,2,3]
29+
a.delete_first_each([ 4]).assert == [1,1,2,3]
30+
a.assert == [1,1,2,3]
31+
end
32+
end
33+
34+
method :delete_first_each! do
35+
test do
36+
a = [1,1,2,3]
37+
a.delete_first_each!([1]).assert == [ 1,2,3]
38+
a.assert == [ 1,2,3]
39+
end
40+
end
41+
end

0 commit comments

Comments
 (0)