Looks like JavaScript, feels like Ruby, and it is a script language fitting in C programmers.
This project is maintained by Kray-G
An array object is a set of sequential objects like [1,2,3]
.
An array itself can be inside an array, and it can be nested like [[1,2,3],3,[2,[3,4]]]
.
There is a special object named as Array
.
The methods of Array
can be used for an array and an object directly.
Note that it will be used also for an object.
Method | Meaning |
---|---|
Array.length(ary) |
Returns the length of an array. |
Array.join(ary, delim) |
Returns the string of joining array items by delim . |
Array.keySet(obj) |
Returns the array of keys. |
Array.push(ary, val) |
Appends the value to the tail of an array. |
Array.pop(ary) |
Takes the value from the tail of an array. |
Array.unshift(ary, val) |
Appends the value to the head of an array. |
Array.shift(ary) |
Takes the value from the head of an array. |
Array.reverse(ary) |
Returns the array of items to reverse the order. |
Array.flatten(ary, level) |
Flattens an array until specified level. |
Array.toString(ary) |
Same as "[" + ary.join(', ') + "]" . |
Array.each(ary, callback) |
Loop and call callback . |
Array.map(ary, callback) |
Returns an array having items mapped by callback . |
Array.filter(ary, callback) |
Returns an array having items filtered by callback . |
Array.reduce(ary, callback, initer) |
Returns an array having items applied by callback(r, initer) to each item. |
Array.sort(ary, cmpfunc) |
Returns a sorted array by cmpfunc . |
Array.shuffle(ary) |
Shuffles items in an array. |
Array.take(n) |
Takes the specified number of items from the head. |
Array.takeWhile(n, callback) |
Takes items while a callback returns true. |
Array.drop(n) |
Drops the specified number of items from the head. |
Array.dropWhile(n, callback) |
Drops items while a callback returns true. |
For example, the following 2 examples mean the same.
Array.length(ary);
ary.length();
Array can be lvalue like the following.
[a, b] = [1, 2, 3];
This style can be used also in a declaration.
var [a, b] = [1, 2, 3];
You can swap the values of variables by this mechanism like this.
[a, b] = [b, a]; // swap
If you don’t have to use a part of element, you can skip the parameter of lvalue.
[a, , b] = [1, 2, 3]; // a == 1, b == 3
You can also skip the first parameter.
[, a, b] = [1, 2, 3]; // a == 2, b == 3
If you put an array style in the argument list, an array of function call argument is separated and set the value to each variable in array list. To understand it easily, see example below.
function func([a, b], c) {
return a + b + c;
}
func([1, 2], 3); // => 6
You can also use an object in the argument list like an array. See example below.
function func({y, z}, c) {
return y + z + c;
}
func({x: 100, y: 200, z: 300}, 3); // => 503
System.println([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[a, b] = [1, 2, 3];
System.println([a, b]);
[1, 2]
var [a, b] = [1, 2, 3];
System.println([a, b]);
[1, 2]
var a = 1, b = 2;
[a, b] = [b, a];
System.println([a, b]);
[2, 1]
[a, , b] = [1, 2, 3]; // a == 1, b == 3
System.println([a, b]);
[, a, b] = [1, 2, 3]; // a == 2, b == 3
System.println([a, b]);
[1, 3]
[2, 3]
function func([a, b], c) {
return a + b + c;
}
System.println(func([1, 2], 3));
6
function func([, a, , b], c) {
return a + b + c;
}
System.println(func([1, 2, 3, 4], 3));
9
function func({y, z}, c) {
return y + z + c;
}
System.println(func({x: 100, y: 200, z: 300}, 3));
503
var r = (1..10).toArray().take(3);
System.println(r);
[1, 2, 3]
var r = (1..10).toArray().takeWhile({ => _1 < 7 });
System.println(r);
[1, 2, 3, 4, 5, 6]
var r = (1..10).toArray().drop(3);
System.println(r);
[4, 5, 6, 7, 8, 9, 10]
var r = (1..10).toArray().dropWhile({ => _1 < 7 });
System.println(r);
[7, 8, 9, 10]