From d1313f1f6c90f8209ec539d2d3f035e65dcdc04b Mon Sep 17 00:00:00 2001 From: minjaesong Date: Fri, 7 May 2021 17:25:36 +0900 Subject: [PATCH] haskell-inspired head/last/tail/init function for JS Array --- assets/JS_INIT.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/assets/JS_INIT.js b/assets/JS_INIT.js index 9c609f2..40212cc 100644 --- a/assets/JS_INIT.js +++ b/assets/JS_INIT.js @@ -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 //