Skip to content

Commit

Permalink
Issue with v2v bin operation and indexes (SciRuby#501)
Browse files Browse the repository at this point in the history
 - added a utility method to fall back to string-sort
   in case sort raise error
 - this metod called in v2v_binary method
 - impending test removed and passes
  • Loading branch information
cyrillefr committed Nov 2, 2020
1 parent 3ac3d15 commit b3f980f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
13 changes: 13 additions & 0 deletions lib/daru/helpers/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,18 @@ def array_of?(array, match)
!array.empty? &&
array.all? { |el| match === el } # rubocop:disable Style/CaseEquality
end

def sort_composite_data(array)
array.sort
rescue ArgumentError, TypeError => msg
case msg.to_s
when /comparison of Symbol with String failed/,
/comparison of Symbol with \d+ failed/,
/comparison of String with :.* failed/,
/comparison of Integer with :.* failed/,
/no implicit conversion from nil to integer/
return array.sort_by(&:to_s)
end
end
end
end
2 changes: 1 addition & 1 deletion lib/daru/maths/arithmetic/vector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def v2o_binary operation, other

def v2v_binary operation, other, opts={}
# FIXME: why the sorting?.. - zverok, 2016-05-18
index = (@index.to_a | other.index.to_a).sort
index = ArrayHelper.sort_composite_data(@index.to_a | other.index.to_a)

elements = index.map do |idx|
this = self.index.include?(idx) ? self[idx] : nil
Expand Down
2 changes: 1 addition & 1 deletion spec/maths/arithmetic/vector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
end

it "appropriately adds vectors with numeric and non-numeric indexes" do
pending "Need an alternate index implementation?"
# pending "Need an alternate index implementation?"
v1 = Daru::Vector.new([1,2,3])
v2 = Daru::Vector.new([1,2,3], index: [:a,:b,:c])

Expand Down

0 comments on commit b3f980f

Please sign in to comment.