Sha256: 2944ed889318ffacbd1097796435c740daae827b7e37a25e0071a1e9f73005eb
Contents?: true
Size: 1.26 KB
Versions: 1
Compression:
Stored size: 1.26 KB
Contents
# frozen_string_literal: true module Combinatorics module Permute # # @author duper <super@manson.vistech.net> # # @since 0.4.0 # module Mixin # # Enumerate distinct r-permutations for a particular sequence of # elements. # # @param [Fixnum] r # Length of permuted subsets to return. # # @yield [permutation] # If a block is given, it will be passed each k-permutation. # # @yieldparam [Array] permutation # A k-permutation of the elements from `self`. # # @return [Enumerator] # If no block is given, an Enumerator of the k-permutations of # elements from `self` is returned. # # @raise [TypeError] # `self` must be Enumerable. # # @example # [1, 2, 3].permute(2).to_a # # => [[1, 2], [1, 3], # # [2, 1], [2, 3], # # [3, 1], [3, 2]] # # @see http://rubydoc.info/stdlib/core/Array#permutation-instance_method # def permute(r,&block) return enum_for(:permute,r) unless block unless kind_of?(Enumerable) raise(TypeError,"#{inspect} must be Enumerable") end self.to_a.permutation(r,&block) end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
combinatorics-0.5.0 | lib/combinatorics/permute/mixin.rb |