続々: n 番目の組み合わせ

Shiro さん版を受けて、SequenceableCollection >> #combinations:atATimeAt: を改良してみました。

SequenceableCollection >> combinations: digits atATimeAt: target
   | size lastIndex |
   size _ self size.
   (size < digits or: [digits < 1]) ifTrue: [^ self species new].
   lastIndex _ size - 1 take: digits - 1.
   ^ lastIndex >= target
      ifTrue: [(self first: 1), (self allButFirst combinations: digits - 1 atATimeAt: target)]
      ifFalse: [self allButFirst combinations: digits atATimeAt: target - lastIndex]

最初っからこう書ければ、かっこいいんですけどねぇ…。orz