Files
smnp-kt/modules/collection/src/main/resources/list.mus

102 lines
2.0 KiB
Plaintext

function flatten(...lists: list) {
return _flatten(lists as list ^ _flatten(list, []), []);
}
function _flatten(list: list, output: list) {
list as element ^ {
if (typeOf(element) == "list") {
output = _flatten(element, output);
} else {
output = output + [element];
}
}
return output;
}
function shuffle(list: list) {
shuffled = list;
list.size-1 as first ^ {
second = random(0, list.size);
shuffled = shuffled.swap(first, second);
}
return shuffled;
}
extend list {
function flatten() {
return flatten(this);
}
function contains(value) {
return (this as item ^ item % item == value).size > 0;
}
function indexOf(value) {
this as (item, index) ^ {
if(item == value) {
return index;
}
}
return -1;
}
function join(separator: string = ", ") {
output = ""
this as (item, index) ^ {
output = output + item;
if (index < this.size - 1) {
output = output + separator;
}
}
return output;
}
function isEmpty() {
return this.size == 0;
}
function isNotEmpty() {
return not this.isEmpty();
}
function dropIndex(index: int) {
output = [];
i = 0;
this as item ^ {
if(index != i) {
output = output + [this.get(i)];
}
i = i + 1;
}
return output;
}
function put(index: int, value) {
return (index as i ^ this.get(i)) + [value] + ((this.size-index) as i ^ this.get(i+index));
}
function replace(index: int, value) {
return this
.dropIndex(index)
.put(index, value);
}
function swap(a: int, b: int) {
A = this.get(a);
B = this.get(b);
return this
.replace(a, B)
.replace(b, A);
}
function shuffle() {
return shuffle(this);
}
}