-
Notifications
You must be signed in to change notification settings - Fork 2
Each_with_index #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Each_with_index #36
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,61 +112,17 @@ def self.convert(matrix) | |
fast_matrix | ||
end | ||
|
||
# | ||
# | ||
# Yields all elements of the matrix, starting with those of the first row | ||
# | ||
# Matrix[ [1,2], [3,4] ].each { |e| puts e } | ||
# # => prints the numbers 1 to 4 | ||
def each(which = :all) # :yield: e | ||
return to_enum :each, which unless block_given? | ||
case which | ||
when :all | ||
(0...row_count).each do |i| | ||
(0...column_count).each do |j| | ||
yield self[i, j] | ||
end | ||
end | ||
when :diagonal | ||
(0...[row_count, column_count].min).each do |i| | ||
yield self[i, i] | ||
end | ||
when :off_diagonal | ||
(0...row_count).each do |i| | ||
(0...column_count).each do |j| | ||
if i != j | ||
yield self[i, j] | ||
end | ||
end | ||
end | ||
when :lower | ||
(0...row_count).each do |i| | ||
(0..[i,column_count-1].min).each do |j| | ||
yield self[i, j] | ||
end | ||
end | ||
when :strict_lower | ||
(1...row_count).each do |i| | ||
(0...[i,column_count].min).each do |j| | ||
yield self[i, j] | ||
end | ||
end | ||
when :strict_upper | ||
(0...row_count).each do |i| | ||
(i+1...column_count).each do |j| | ||
yield self[i, j] | ||
end | ||
end | ||
when :upper | ||
(0...row_count).each do |i| | ||
(i...column_count).each do |j| | ||
yield self[i, j] | ||
end | ||
end | ||
else | ||
raise ArgumentError, "expected #{which.inspect} to be one of :all, :diagonal, :off_diagonal, :lower, :strict_lower, :strict_upper or :upper" | ||
end | ||
self | ||
def each(which = :all) # :yield: e | ||
return to_enum :each, which unless block_given? | ||
|
||
each_with_index(which){ |elem, _, _| yield elem} | ||
end | ||
|
||
# | ||
# Same as #each, but the row index and column index in addition to the element | ||
# | ||
|
@@ -179,17 +135,85 @@ def each(which = :all) # :yield: e | |
# # 3 at 1, 0 | ||
# # 4 at 1, 1 | ||
# | ||
def each_with_index | ||
raise NotSupportedError unless block_given? | ||
def each_with_index(which = :all) # :yield: e, row, column | ||
return to_enum :each_with_index, which unless block_given? | ||
case which | ||
when :all | ||
each_all{|i, j| yield self[i, j], i, j} | ||
when :diagonal | ||
each_diagonal{|i, j| yield self[i, j], i, j} | ||
when :off_diagonal | ||
each_off_diagonal{|i, j| yield self[i, j], i, j} | ||
when :lower | ||
each_lower{|i, j| yield self[i, j], i, j} | ||
when :strict_lower | ||
each_strict_lower{|i, j| yield self[i, j], i, j} | ||
when :strict_upper | ||
each_strict_upper{|i, j| yield self[i, j], i, j} | ||
when :upper | ||
each_upper{|i, j| yield self[i, j], i, j} | ||
else | ||
raise ArgumentError, "expected #{which.inspect} to be one of :all, :diagonal, :off_diagonal, :lower, :strict_lower, :strict_upper or :upper" | ||
RobolabGs2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
self | ||
end | ||
|
||
def each_all(&block) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This methods don't return items of the matrix -> they aren't |
||
(0...row_count).each do |i| | ||
(0...column_count).each do |j| | ||
yield self[i, j], i, j | ||
block[i,j] | ||
end | ||
end | ||
self | ||
end | ||
|
||
def each_diagonal(&block) | ||
(0...[row_count, column_count].min).each do |i| | ||
block[i, i] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you don't pass a block anywhere, you should use |
||
end | ||
end | ||
|
||
def each_off_diagonal(&block) | ||
(0...row_count).each do |i| | ||
(0...column_count).each do |j| | ||
if i != j | ||
block[i, j] | ||
end | ||
end | ||
end | ||
end | ||
|
||
def each_lower(&block) | ||
(0...row_count).each do |i| | ||
(0..[i,column_count-1].min).each do |j| | ||
block[i, j] | ||
end | ||
end | ||
end | ||
|
||
def each_strict_lower(&block) | ||
(1...row_count).each do |i| | ||
(0...[i,column_count].min).each do |j| | ||
block[i, j] | ||
end | ||
end | ||
end | ||
|
||
def each_strict_upper(&block) | ||
(0...row_count).each do |i| | ||
(i+1...column_count).each do |j| | ||
block[i, j] | ||
end | ||
end | ||
end | ||
|
||
def each_upper(&block) | ||
(0...row_count).each do |i| | ||
(i...column_count).each do |j| | ||
block[i, j] | ||
end | ||
end | ||
end | ||
|
||
# don't use (Issue#1) | ||
def each_with_index! | ||
(0...row_count).each do |i| | ||
|
Uh oh!
There was an error while loading. Please reload this page.