2010-04-03 10:39:32 -04:00
|
|
|
# Slice.
|
2010-07-25 01:23:37 -04:00
|
|
|
array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
2009-12-25 19:35:57 -05:00
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
a = array[7..9]
|
|
|
|
b = array[2...4]
|
2009-12-25 19:35:57 -05:00
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
result = a.concat(b).join(' ')
|
2009-12-25 19:35:57 -05:00
|
|
|
|
2010-02-16 19:45:25 -05:00
|
|
|
ok result is "7 8 9 2 3"
|
2010-01-05 22:49:51 -05:00
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
a = [0, 1, 2, 3, 4, 5, 6, 7]
|
2010-04-03 10:39:32 -04:00
|
|
|
deepEqual a[2...6], [2, 3, 4, 5]
|
|
|
|
|
2010-01-05 22:49:51 -05:00
|
|
|
|
2010-06-28 20:26:31 -04:00
|
|
|
# Ranges.
|
2010-07-25 01:23:37 -04:00
|
|
|
countdown = [10..1].join(' ')
|
2010-02-16 19:45:25 -05:00
|
|
|
ok countdown is "10 9 8 7 6 5 4 3 2 1"
|
2010-02-07 12:52:07 -05:00
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
a = 1
|
|
|
|
b = 5
|
|
|
|
nums = [a...b]
|
2010-06-28 20:26:31 -04:00
|
|
|
ok nums.join(' ') is '1 2 3 4'
|
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
b = -5
|
|
|
|
nums = [a..b]
|
2010-06-28 20:26:31 -04:00
|
|
|
ok nums.join(' ') is '1 0 -1 -2 -3 -4 -5'
|
|
|
|
|
2010-02-07 12:52:07 -05:00
|
|
|
|
2010-04-03 10:39:32 -04:00
|
|
|
# Expression-based range.
|
2010-07-25 01:23:37 -04:00
|
|
|
array = [(1+5)..1+9]
|
2010-03-21 10:59:41 -04:00
|
|
|
ok array.join(' ') is "6 7 8 9 10"
|
|
|
|
|
2010-07-27 23:02:26 -04:00
|
|
|
array = [5..1]
|
|
|
|
ok array.join(' ') is '5 4 3 2 1'
|
|
|
|
|
2010-03-21 10:59:41 -04:00
|
|
|
|
2010-04-03 10:39:32 -04:00
|
|
|
# String slicing (at least on Node).
|
2010-07-25 01:23:37 -04:00
|
|
|
hello = "Hello World"
|
2010-03-21 10:59:41 -04:00
|
|
|
|
|
|
|
ok hello[1...1] is ""
|
|
|
|
ok hello[1..1] is "e"
|
|
|
|
ok hello[1...5] is "ello"
|
|
|
|
ok hello[0..4] is "Hello"
|
|
|
|
|
2010-04-03 10:39:32 -04:00
|
|
|
|
|
|
|
# Splice literals.
|
2010-07-25 01:23:37 -04:00
|
|
|
array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
2010-04-03 10:39:32 -04:00
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
array[5..10] = [0, 0, 0]
|
2010-04-03 10:39:32 -04:00
|
|
|
|
|
|
|
ok array.join(' ') is '0 1 2 3 4 0 0 0'
|
|
|
|
|
2010-03-30 20:06:44 -04:00
|
|
|
|