2010-12-29 00:48:54 -05:00
|
|
|
# Slicing and Splicing
|
|
|
|
# --------------------
|
2010-12-16 05:14:57 -05:00
|
|
|
|
2010-12-29 14:06:57 -05:00
|
|
|
# * Slicing
|
|
|
|
# * Splicing
|
|
|
|
|
2010-12-16 05:14:57 -05:00
|
|
|
# shared array
|
|
|
|
shared = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
|
|
|
2010-12-29 00:48:54 -05:00
|
|
|
#### Slicing
|
2010-12-16 05:14:57 -05:00
|
|
|
|
|
|
|
test "basic slicing", ->
|
2010-12-18 14:45:32 -05:00
|
|
|
arrayEq [7, 8, 9] , shared[7..9]
|
|
|
|
arrayEq [2, 3] , shared[2...4]
|
|
|
|
arrayEq [2, 3, 4, 5], shared[2...6]
|
2010-12-16 05:14:57 -05:00
|
|
|
|
|
|
|
test "slicing with variables as endpoints", ->
|
2010-12-16 20:21:29 -05:00
|
|
|
[a, b] = [1, 4]
|
2010-12-18 14:45:32 -05:00
|
|
|
arrayEq [1, 2, 3, 4], shared[a..b]
|
|
|
|
arrayEq [1, 2, 3] , shared[a...b]
|
2010-12-16 05:14:57 -05:00
|
|
|
|
|
|
|
test "slicing with expressions as endpoints", ->
|
2010-12-16 20:21:29 -05:00
|
|
|
[a, b] = [1, 3]
|
2010-12-18 14:45:32 -05:00
|
|
|
arrayEq [2, 3, 4, 5, 6], shared[(a+1)..2*b]
|
|
|
|
arrayEq [2, 3, 4, 5] , shared[a+1...(2*b)]
|
2010-12-16 05:14:57 -05:00
|
|
|
|
|
|
|
test "unbounded slicing", ->
|
2010-12-18 14:45:32 -05:00
|
|
|
arrayEq [7, 8, 9] , shared[7..]
|
|
|
|
arrayEq [8, 9] , shared[-2..]
|
|
|
|
arrayEq [9] , shared[-1...]
|
|
|
|
arrayEq [0, 1, 2] , shared[...3]
|
|
|
|
arrayEq [0, 1, 2, 3], shared[..-7]
|
2010-12-16 20:21:29 -05:00
|
|
|
|
2010-12-18 14:45:32 -05:00
|
|
|
arrayEq shared , shared[..-1]
|
|
|
|
arrayEq shared[0..8], shared[...-1]
|
2010-12-16 20:21:29 -05:00
|
|
|
|
|
|
|
for a in [-shared.length..shared.length]
|
2010-12-18 14:45:32 -05:00
|
|
|
arrayEq shared[a..] , shared[a...]
|
2010-12-16 20:21:29 -05:00
|
|
|
for a in [-shared.length+1...shared.length]
|
2010-12-18 14:45:32 -05:00
|
|
|
arrayEq shared[..a][...-1] , shared[...a]
|
2010-12-16 05:14:57 -05:00
|
|
|
|
|
|
|
test "#930, #835, #831, #746 #624: inclusive slices to -1 should slice to end", ->
|
2010-12-18 14:45:32 -05:00
|
|
|
arrayEq shared, shared[0..-1]
|
|
|
|
arrayEq shared, shared[..-1]
|
|
|
|
arrayEq shared.slice(1,shared.length), shared[1..-1]
|
2010-12-16 05:14:57 -05:00
|
|
|
|
|
|
|
test "string slicing", ->
|
|
|
|
str = "abcdefghijklmnopqrstuvwxyz"
|
|
|
|
ok str[1...1] is ""
|
|
|
|
ok str[1..1] is "b"
|
|
|
|
ok str[1...5] is "bcde"
|
|
|
|
ok str[0..4] is "abcde"
|
|
|
|
ok str[-5..] is "vwxyz"
|
|
|
|
|
|
|
|
|
2010-12-29 00:48:54 -05:00
|
|
|
#### Splicing
|
2010-12-16 05:14:57 -05:00
|
|
|
|
|
|
|
test "basic splicing", ->
|
|
|
|
ary = [0..9]
|
|
|
|
ary[5..9] = [0, 0, 0]
|
2010-12-18 14:45:32 -05:00
|
|
|
arrayEq [0, 1, 2, 3, 4, 0, 0, 0], ary
|
2010-12-16 05:14:57 -05:00
|
|
|
|
2010-12-16 20:21:29 -05:00
|
|
|
ary = [0..9]
|
|
|
|
ary[2...8] = []
|
2010-12-18 14:45:32 -05:00
|
|
|
arrayEq [0, 1, 8, 9], ary
|
2010-12-16 20:21:29 -05:00
|
|
|
|
2010-12-16 05:14:57 -05:00
|
|
|
test "unbounded splicing", ->
|
|
|
|
ary = [0..9]
|
|
|
|
ary[3..] = [9, 8, 7]
|
2010-12-18 14:45:32 -05:00
|
|
|
arrayEq [0, 1, 2, 9, 8, 7]. ary
|
2010-12-16 05:14:57 -05:00
|
|
|
|
|
|
|
ary[...3] = [7, 8, 9]
|
2010-12-18 14:45:32 -05:00
|
|
|
arrayEq [7, 8, 9, 9, 8, 7], ary
|
2010-12-16 05:14:57 -05:00
|
|
|
|
2010-12-16 20:21:29 -05:00
|
|
|
test "splicing with variables as endpoints", ->
|
|
|
|
[a, b] = [1, 8]
|
|
|
|
|
|
|
|
ary = [0..9]
|
|
|
|
ary[a..b] = [2, 3]
|
2010-12-18 14:45:32 -05:00
|
|
|
arrayEq [0, 2, 3, 9], ary
|
2010-12-16 05:14:57 -05:00
|
|
|
|
2010-12-16 20:21:29 -05:00
|
|
|
ary = [0..9]
|
|
|
|
ary[a...b] = [5]
|
2010-12-18 14:45:32 -05:00
|
|
|
arrayEq [0, 5, 8, 9], ary
|
2010-12-16 20:21:29 -05:00
|
|
|
|
2010-12-20 23:41:58 -05:00
|
|
|
test "splicing with expressions as endpoints", ->
|
|
|
|
[a, b] = [1, 3]
|
|
|
|
|
|
|
|
ary = [0..9]
|
|
|
|
ary[ a+1 .. 2*b+1 ] = [4]
|
|
|
|
arrayEq [0, 1, 4, 8, 9], ary
|
2010-12-22 01:39:58 -05:00
|
|
|
|
|
|
|
ary = [0..9]
|
|
|
|
ary[a+1...2*b+1] = [4]
|
|
|
|
arrayEq [0, 1, 4, 7, 8, 9], ary
|
2010-12-23 17:02:46 -05:00
|
|
|
|
|
|
|
test "splicing to the end, against a one-time function", ->
|
|
|
|
ary = null
|
|
|
|
fn = ->
|
|
|
|
if ary
|
|
|
|
throw 'err'
|
|
|
|
else
|
|
|
|
ary = [1, 2, 3]
|
|
|
|
|
|
|
|
fn()[0..] = 1
|
|
|
|
|
|
|
|
arrayEq ary, [1]
|
2010-12-23 17:46:34 -05:00
|
|
|
|
|
|
|
test "the return value of a splice literal should be the RHS", ->
|
|
|
|
ary = [0, 0, 0]
|
|
|
|
eq (ary[0..1] = 2), 2
|
|
|
|
|
|
|
|
ary = [0, 0, 0]
|
|
|
|
eq (ary[0..] = 3), 3
|
|
|
|
|
|
|
|
arrayEq [ary[0..0] = 0], [0]
|