Sha256: dfe2d7a22a1a9cfa7cafa8cd64351b96cbd1df4247bb266ecea262c1228ad8d5

Contents?: true

Size: 876 Bytes

Versions: 4

Compression:

Stored size: 876 Bytes

Contents

# frozen_string_literal: true

module Refinements
  # Provides additional enhancements to the Array primitive.
  module Arrays
    refine Array do
      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(&block) = [last, *self, first].each_cons(3, &block)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
refinements-8.5.2 lib/refinements/arrays.rb
refinements-8.5.1 lib/refinements/arrays.rb
refinements-8.5.0 lib/refinements/arrays.rb
refinements-8.4.1 lib/refinements/arrays.rb