From c36d5b39d11fd1b9c2354c1f0b8bc32d7cfb8fd7 Mon Sep 17 00:00:00 2001 From: Dov Murik Date: Wed, 3 Feb 2016 15:19:35 -0500 Subject: [PATCH] groovy, java, scala: Fix (first nil) and (rest nil) --- groovy/core.groovy | 4 ++-- java/src/main/java/mal/core.java | 12 ++++++++++-- scala/core.scala | 11 ++++++++--- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/groovy/core.groovy b/groovy/core.groovy index d266fad2..175e41dc 100644 --- a/groovy/core.groovy +++ b/groovy/core.groovy @@ -91,8 +91,8 @@ class core { "cons": { a -> [a[0]] + (a[1] as List) }, "concat": core.&do_concat, "nth": core.&do_nth, - "first": { a -> a[0].size() == 0 ? null : a[0][0] }, - "rest": { a -> a[0].drop(1) }, + "first": { a -> a[0] == null || a[0].size() == 0 ? null : a[0][0] }, + "rest": { a -> a[0] == null ? [] as List : a[0].drop(1) }, "empty?": { a -> a[0] == null || a[0].size() == 0 }, "count": { a -> a[0] == null ? 0 : a[0].size() }, "apply": core.&do_apply, diff --git a/java/src/main/java/mal/core.java b/java/src/main/java/mal/core.java index d93566d6..5f76f64f 100644 --- a/java/src/main/java/mal/core.java +++ b/java/src/main/java/mal/core.java @@ -373,14 +373,22 @@ public class core { static MalFunction first = new MalFunction() { public MalVal apply(MalList a) throws MalThrowable { - MalList ml = ((MalList)a.nth(0)); + MalVal exp = a.nth(0); + if (exp == Nil) { + return Nil; + } + MalList ml = ((MalList)exp); return ml.size() > 0 ? ml.nth(0) : Nil; } }; static MalFunction rest = new MalFunction() { public MalVal apply(MalList a) throws MalThrowable { - MalList ml = ((MalList)a.nth(0)); + MalVal exp = a.nth(0); + if (exp == Nil) { + return new MalList(); + } + MalList ml = ((MalList)exp); return ml.rest(); } }; diff --git a/scala/core.scala b/scala/core.scala index df6156b4..210204bf 100644 --- a/scala/core.scala +++ b/scala/core.scala @@ -83,13 +83,18 @@ object core { } def first(a: List[Any]): Any = { - val lst = a(0).asInstanceOf[MalList].value - if (lst.length > 0) lst(0) else null + a(0) match { + case null => null + case ml: MalList => { + val lst = ml.value + if (lst.length > 0) lst(0) else null + } + } } def rest(a: List[Any]): Any = { a(0) match { - case null => true + case null => _list() case ml: MalList => _list(ml.drop(1).value:_*) } } -- 2.20.1