Sha256: b8d8b0622d902287ec55abaf6bdb64f3ea821c9ef6f5dddd499052243d4d8529

Contents?: true

Size: 1.36 KB

Versions: 4

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true
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::Words["foo", "bar", baz", "qux"]
      #   wordlist2 = Wordlist::Words["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

4 entries across 4 versions & 1 rubygems

Version Path
wordlist-1.1.0 lib/wordlist/operators/subtract.rb
wordlist-1.0.3 lib/wordlist/operators/subtract.rb
wordlist-1.0.2 lib/wordlist/operators/subtract.rb
wordlist-1.0.1 lib/wordlist/operators/subtract.rb