5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Smalltalkでスターリンソート

Posted at

Smalltalkの練習がてら、かなり賞味期限が切れたネタですがスターリンソートを実装してみました。
Advent Calendarの期限は過ぎていますが、賑やかしにスターリンの誕生日である12月18日のSmalltalk Advent Calendarに載せてみます。

実装内容

immutableなArrayの場合は自身を変えられないのでstalinSortではなくstalinSortedというメソッド名にしています。

SequencableCollection.st
stalinSorted
	^ self copy stalinSorted: [ :a :b | a <= b ]
SequencableCollection.st
stalinSorted: aBlock
	| result |
	result := self copyFrom: 1 to: 1.
	(self copyFrom: 2 to: self size) doWithIndex: [ :each :index |
		(aBlock value: result last value: each)
			ifTrue: [ result := result copyWith: each ]
			ifFalse: [ ]
	].
	^ result

いまだにSmalltalk的な書き方がよくわかりません……。

実行結果

#(6 2 5 7 3 8 8 4) stalinSorted. "#(6 7 8 8)"
#(1 2 1 3 2 3 4) stalinSorted. "#(1 2 3 3 4)"

一応期待通り(?)粛清できているようです。

5
1
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?