Method: Fable::InkList#list_with_subrange

Defined in:
lib/fable/ink_list.rb

#list_with_subrange(min_bound, max_bound) ⇒ Object

Returns a sublist with the elements given in the minimum & maximum bounds. The bounds can either be ints, which are indicies into the entire (sorted) list, or they can be InkLists themsevles. These are intended to be single-item lists, so you can specify the upper & lower bounds. If you pass in multi-item lists, it’ll use the minimum and maximum items in those lists, respectively.



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/fable/ink_list.rb', line 367

def list_with_subrange(min_bound, max_bound)
  return self.class.new if self.list.empty?

  ordered = self.ordered_items

  min_value = 0
  max_value = Float::INFINITY

  if min_bound.is_a?(Numeric)
    min_value = min_bound
  elsif min_bound.is_a?(InkList) && !min_bound.list.empty?
    min_value = min_bound.min_item[1]
  end

  if max_bound.is_a?(Numeric)
    max_value = max_bound
  elsif max_bound.is_a?(InkList) && !max_bound.list.empty?
    max_value = max_bound.max_item[1]
  end

  sublist = self.class.new
  sublist.set_initial_origin_names(origin_names)
  ordered.each do |item, int_value|
    if int_value >= min_value && int_value <= max_value
      sublist.list[item] = int_value
    end
  end

  sublist
end