From: muraken@... Date: 2018-03-08T09:26:28+00:00 Subject: [ruby-core:86040] [Ruby trunk Feature#4907][Closed] enumerable#permutation and combination Issue #4907 has been updated by mrkn (Kenta Murata). Status changed from Assigned to Closed No use case is shown, so I close this issue. ---------------------------------------- Feature #4907: enumerable#permutation and combination https://bugs.ruby-lang.org/issues/4907#change-70902 * Author: neleai (Ondrej Bilka) * Status: Closed * Priority: Normal * Assignee: mrkn (Kenta Murata) * Target version: ---------------------------------------- Hello Methods permutation and combination are defined for array but it make more sense to define them for enumerable. Here is sample implementation which for simplicity works only with blocks. Note that implementation works lazily. It changes traversal order but documentation states that order is unspecified. module Enumerable def perm(n) comb(n){|ary| ary.permutation{|p| yield(p)} } end def comb(n,&m) ary=[] e=to_enum.with_index _comb(e,n-1,1.0/0.0,ary,m) end def _comb(e,n,bound,ary,m) e.each{|el,i| return if i>=bound ary[n]=el if n==0 m.call(ary) else _comb(e,n-1,i,ary,m) end } end end [1,2,4,3,5].comb(2){|a| puts a.inspect} (1..4).comb(2){|a| puts a.inspect} (1..4).perm(2){|a| puts a.inspect} -- https://bugs.ruby-lang.org/ Unsubscribe: