Class: Peruby::Op::Sort

Inherits:
Base
  • Object
show all
Defined in:
lib/peruby/op/loop.rb

Overview

Stable Perl sort with optional block or named comparator.

Instance Method Summary collapse

Methods inherited from Base

#argument_cells, #local_slot, #lvalue, #run_subroutine, #to_callable, #warning_directive?

Constructor Details

#initialize(comparator, list, package) ⇒ Sort

Returns a new instance of Sort.



43
44
45
46
47
# File 'lib/peruby/op/loop.rb', line 43

def initialize(comparator, list, package)
  @comparator = comparator
  @list = list
  @package = package
end

Instance Method Details

#run(env, context) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/peruby/op/loop.rb', line 49

def run(env, context)
  values = @list.run(env, :list)
  sorted = values.each_with_index.sort do |(left, left_index), (right, right_index)|
    comparison = compare(left, right, env)
    comparison.zero? ? left_index <=> right_index : comparison
  end.map(&:first)
  context == :list ? sorted : nil
end