haskell-inspired head/last/tail/init function for JS Array

This commit is contained in:
minjaesong
2021-05-07 17:25:36 +09:00
parent 1b98a19de7
commit d1313f1f6c

View File

@@ -321,6 +321,19 @@ if (!Object.entries) {
return resArray;
};
}
// haskell-inspired array functions
Array.prototype.head = function() {
return this[0]
}
Array.prototype.last = function() {
return this[this.length - 1]
}
Array.prototype.tail = function() {
return this.slice(1)
}
Array.prototype.init = function() {
return this.slice(0, this.length - 1)
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// NOTE TO PROGRAMMERS: this JS_INIT script does not, and must not be invoked with strict mode //