LoginSignup
0
0

More than 5 years have passed since last update.

Smalltalk で回転演算を実装する

Last updated at Posted at 2016-01-02

 ふと Smalltalk で「回転演算ってどうやるんだろうなー?」と思ったんですが、軽く探してみて見つからなかったので、自前実装するとしたらこんな感じでしょうか?
 配列(もちろん文字列も可)を右に k 回転させます。

| array k middle first last result |
array := #(0 1 2 3 4 5 6).
k := 3.
middle := array size - k + 1.
first := (array copyFrom: 1 to: middle - 1) reverse.
last := (array copyFrom: middle to: array size) reverse.
result := (first, last) reverse.

結果は以下のようになります。

#(4 5 6 0 1 2 3)

でもやっぱり、そのものズバリのものがあるような気がしますが…。

0
0
2

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
0
0