Sha256: ec55ab2b553a16f8c5b0253bc94fd39fe75edcc311fddec22f8c421fd237d653

Contents?: true

Size: 1.61 KB

Versions: 4

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true
require 'wordlist/operators/binary_operator'
require 'wordlist/operators/product'

module Wordlist
  module Operators
    #
    # Lazily enumerates over every combination of words in the wordlist.
    #
    # @since 1.0.0
    #
    class Power < BinaryOperator

      # The product of the wordlist with itself.
      #
      # @return [Product]
      attr_reader :wordlists

      alias exponent right

      #
      # Initializes the power operator.
      #
      # @param [Enumerable] wordlist
      #
      # @param [Integer] exponent
      #
      def initialize(wordlist,exponent)
        super(wordlist,exponent)

        @wordlists = wordlist

        (exponent - 1).times do
          @wordlists = Product.new(wordlist,@wordlists)
        end
      end

      #
      # Enumerates over every combination of words from the wordlist.
      #
      # @yield [string]
      #   The given block will be passed each combination of words from the
      #   wordlist.
      #
      # @yieldparam [String] string
      #   A combination of words from the wordlist.
      #
      # @return [Enumerator]
      #   If no block is given, an Enumerator object will be returned.
      #
      # @example
      #   wordlist = Wordlist::Words["foo", "bar"]
      #   (wordlist ** 3).each do |word|
      #     puts word
      #   end
      #   # foofoofoo
      #   # foofoobar
      #   # foobarfoo
      #   # foobarbar
      #   # barfoofoo
      #   # barfoobar
      #   # barbarfoo
      #   # barbarbar
      #
      # @api public
      #
      def each(&block)
        @wordlists.each(&block)
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

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