sequence の要素を item という
var weekDays = ["Mon","Tue","Wed","Thu","Fri"];
コンパイラは item から型を判断する。上記の場合コンパイラは
String と判断する
型を明記することもできる
var weekDays: String[] = ["Mon","Tue","Wed","Thu","Fri"];
他のsequence を使って宣言することができる
var days = [weekDays, ["Sat","Sun"]];
// var = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"]
1 から 100 までの item の sequence を簡単に作ることが出来る
var nums = [1..100];
条件式を使って item を選択することができる
var nums = [1,2,3,4,5];
var numsGreaterThanTwo = nums[n | n > 2];
// numsGreaterThanTwo = [3,4,5]
sequence の長さは sizeof で得られる
var days = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"];
println(sizeof days);
// 7
item を追加するには insert を使う
var days = ["Mon"];
insert "Tue" into days;
insert "Fri" into days;
insert "Sat" into days;
insert "Sun" into days;
// days = ["Mon","Tue","Fri","Sat","Sun"]
位置を指定して追加することができる
insert "Thu" before days[2];
insert "wed" after days[1];
item を削除するには delete を使う
delete "Sun" from days;
delete days[0];
delete days; // delete all items
item の順番を逆にするには reverse を使う
var nums = [1..5];
reverse nums; // returns [5, 4, 3, 2, 1]
sequence を比較することができる
2つの sequence の長さが同じで、item が同じとき
2つの sequence は等しいと判断される
var seq1 = [1,2,3,4,5];
var seq2 = [1,2,3,4,5];
println(seq1 == seq2); // true
var seq1 = [1,2,3,4,5];
var seq2 = [1,2,3,4,5,6];
println(seq1 == seq2); // false
var seq1 = [1,2,3,4,5];
var seq2 = [1,3,2,4,5];
println(seq1 == seq2); // false
sequence を slice して新しい sequence を作ることができる
seq[a..b]
var days = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"];
var weekend = days[5..6]; // weekend = ["Sat","Sun"]
seq[a..< b]
var days = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"];
var weekdays = days[0..<5];
// weekend = ["Mon","Tue","Wed","Thu","Fri"]
seq[a..]
var days = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"];
var weekend = days[5..]; // weekend = ["Sat","Sun"]
seq[a..<]
var days = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"];
var days2 = days[0..<];
// var = ["Mon","Tue","Wed","Thu","Fri","Sat"]
0 件のコメント:
コメントを投稿