Skip to content
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

feat(collect!): collected! and tests implemented #27

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

ddrkt
Copy link
Contributor

@ddrkt ddrkt commented Nov 3, 2019

No description provided.

@@ -68,6 +68,17 @@ def test_to_str
assert_equal "FastMatrix::Matrix[[1.0, 2.0], [3.0, 4.0]]", m1.to_str
end

def test_collect
m1 = Matrix[[1, 2], [3, 4]]
assert_equal m1.collect { |el| el.join(',') }, ["1.0,2.0", "3.0,4.0"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. There are usually first - expected value, second - actual in asserts.
  2. Standard matrix shows a different behavior:
irb(main):001:0> require 'matrix'
=> true
irb(main):002:0> m = Matrix[[1, 2], [3, 4]]
=> Matrix[[1, 2], [3, 4]]
irb(main):003:0> m.collect{ |e| e.join(',') }
Traceback (most recent call last):
       15: from C:/Ruby26-x64/bin/irb.cmd:31:in `<main>'
       14: from C:/Ruby26-x64/bin/irb.cmd:31:in `load'
       13: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb:11:in `<top (required)>'
       12: from (irb):3
       11: from C:/Ruby26-x64/lib/ruby/2.6.0/matrix.rb:472:in `collect'
       10: from C:/Ruby26-x64/lib/ruby/2.6.0/matrix.rb:491:in `collect!'
        9: from C:/Ruby26-x64/lib/ruby/2.6.0/matrix.rb:583:in `each_with_index'
        8: from C:/Ruby26-x64/lib/ruby/2.6.0/matrix.rb:583:in `each_with_index'
        7: from C:/Ruby26-x64/lib/ruby/2.6.0/matrix.rb:583:in `each'
        6: from C:/Ruby26-x64/lib/ruby/2.6.0/matrix.rb:584:in `block in each_with_index'
        5: from C:/Ruby26-x64/lib/ruby/2.6.0/matrix.rb:584:in `each_with_index'
        4: from C:/Ruby26-x64/lib/ruby/2.6.0/matrix.rb:584:in `each'
        3: from C:/Ruby26-x64/lib/ruby/2.6.0/matrix.rb:585:in `block (2 levels) in each_with_index'
        2: from C:/Ruby26-x64/lib/ruby/2.6.0/matrix.rb:491:in `block in collect!'
        1: from (irb):3:in `block in irb_binding'
NoMethodError (undefined method `join' for 1:Integer)
irb(main):004:0> m.collect{ |e| e+1 }
=> Matrix[[2, 3], [4, 5]]

@RobolabGs2
Copy link
Member

It would be nice if you make also parameter witch such as in standard.

collect(which = :all)
Returns a matrix that is the result of iteration of the given block over all elements of the matrix. Elements can be restricted by passing an argument:
:all (default): yields all elements
:diagonal: yields only elements on the diagonal
:off_diagonal: yields all elements except on the diagonal
:lower: yields only elements on or below the diagonal
:strict_lower: yields only elements below the diagonal
:strict_upper: yields only elements above the diagonal
:upper: yields only elements on or above the diagonal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants