Class: Put::Debug

Inherits:
Object
  • Object
show all
Defined in:
lib/put/debug.rb

Defined Under Namespace

Classes: Incomparable, Result

Instance Method Summary collapse

Instance Method Details

#call(sorting_arrays) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/put/debug.rb', line 16

def call(sorting_arrays)
  sorting_arrays.sort
  Result.new(success?: true, incomparables: [])
rescue ArgumentError
  # TODO this is O(n^lol)
  incomparables = sorting_arrays.transpose.map.with_index { |comparables, sorting_index|
    comparables.map.with_index { |comparable, comparable_index|
      comparables.map.with_index { |other, other_index|
        next if comparable_index == other_index
        if (comparable <=> other).nil?
          Incomparable.new(
            sorting_index: sorting_index,
            left: comparable,
            left_index: comparable_index,
            left_value: (comparable.value if comparable.is_a?(PutsThing)),
            right: other,
            right_index: other_index,
            right_value: (other.value if other.is_a?(PutsThing))
          )
        end
      }
    }
  }.flatten.compact.uniq { |inc|
    # Remove dupes where two items are incomparable in both <=> directions:
    [inc.sorting_index] + [inc.left_index, inc.right_index].sort
  }
  Result.new(success?: false, incomparables: incomparables)
end