Sha256: 5e6603255afe9e88b54515d30240f1255d8aba73f0c9705b00397bf53f2556ec

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

require "refinements/shared/enumerables/many"

module Refinements
  # Provides additional enhancements to the Array primitive.
  module Arrays
    refine Array do
      import_methods Shared::Enumerables::Many

      def combinatorial?(other) = !other.empty? && size == union(other).size

      def compress = dup.compress!

      def compress!
        compact!
        delete_if { |element| element.respond_to?(:empty?) && element.empty? }
      end

      def excluding(*elements) = self - elements.flatten

      def filter_find(&block) = block ? lazy.map(&block).find(&:itself) : lazy

      def including(*elements) = self + elements.flatten

      def intersperse(*elements) = product([elements]).tap(&:pop).flatten.push(last)

      def maximum(key) = map(&key).max

      def mean = size.zero? ? 0 : sum(0.0) / size

      def minimum(key) = map(&key).min

      def pad(value, max: size) = dup.fill(value, size..(max - 1))

      def ring(&) = [last, *self, first].each_cons(3, &)

      def to_sentence delimiter: ", ", conjunction: "and"
        case length
          when (3..) then "#{self[..-2].join delimiter}#{delimiter}#{conjunction} #{last}"
          when 2 then join " #{conjunction} "
          else join
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
refinements-9.6.0 lib/refinements/arrays.rb