Sha256: cd4a7caa79b292b62a0af6d09ff3c3fc5a195bcd1cc5bac8f4f2beb9b2137c19

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

require 'wordlist/operators/binary_operator'
require 'wordlist/unique_filter'

module Wordlist
  module Operators
    #
    # Lazily enumerates over every word in the first wordlist, that is not in
    # the second wordlist.
    #
    # @since 1.0.0
    #
    class Subtract < BinaryOperator

      #
      # Enumerates over the difference between the two wordlists.
      #
      # @yield [word]
      #   The given block will be passed each word from the first wordlist,
      #   that is not in the second wordlist.
      #
      # @yieldparam [String] word
      #   A word that belongs to first wordlist, but not the second wordlist.
      #
      # @return [Enumerator]
      #   If no block is given, an Enumerator object will be returned.
      #
      # @example
      #   wordlist1 = Wordlist::List["foo", "bar", baz", "qux"]
      #   wordlist2 = Wordlist::List["bar", "qux"]
      #   (wordlist1 - wordlist2).each do |word|
      #     puts word
      #   end
      #   # foo
      #   # baz
      #
      # @api public
      #
      def each
        return enum_for(__method__) unless block_given?

        unique_filter = UniqueFilter.new

        @right.each { |word| unique_filter.add(word) }

        @left.each do |word|
          unless unique_filter.include?(word)
            yield word
          end
        end
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wordlist-1.0.0 lib/wordlist/operators/subtract.rb