From 87ee44163d0f7ec86963cefea51e721ce4eb156c Mon Sep 17 00:00:00 2001 From: mryouse Date: Tue, 6 Jun 2023 19:45:47 -0400 Subject: initial commit of most and last --- chunk.d | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'chunk.d') diff --git a/chunk.d b/chunk.d index 55772dc..21ebb9d 100644 --- a/chunk.d +++ b/chunk.d @@ -45,6 +45,8 @@ abstract class Seq { SeqType type; abstract Value first(); abstract Seq rest(); + abstract Seq most(); + abstract Value last(); abstract int length(); abstract bool isIn(Value val); } @@ -70,6 +72,14 @@ class String : Seq { return new String(this.str[1..$]); } + override Seq most() { + return new String(this.str[0..$ - 1]); + } + + override Value last() { + return makeStringValue(to!string(this.str[$ - 1])); + } + override int length() { return to!int(str.length); } @@ -133,6 +143,22 @@ class List : Seq { return ret; } + override Seq most() { + if (this.inner.length == 0) { + return new List(0); + } + int newLength = to!int(this.inner.length) - 1; + List ret = new List(newLength); + for (int i = 0; i < newLength; i++) { + ret.addItemAtIndex(this.inner[i], i); + } + return ret; + } + + override Value last() { + return this.inner[$ - 1]; // this fails on NIL + } + override int length() { return to!int(this.inner.length); } @@ -195,7 +221,9 @@ enum OpCode { OP_APPEND, OP_FIRST, // No? OP_LENGTH, + OP_LAST, OP_MEMBER, + OP_MOST, OP_REST, // DEFINITIONS/VARIABLES -- cgit v1.2.3