From d1ed8f49d38e1c4fc1cb9e49406cf38d7ec4ffca Mon Sep 17 00:00:00 2001 From: Antony Woods Date: Tue, 7 Apr 2015 10:30:52 +0100 Subject: [PATCH] Re-implemented last function so that it also supports iterators. Added a test to reflect this. --- hy/core/language.hy | 2 +- tests/native_tests/core.hy | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/hy/core/language.hy b/hy/core/language.hy index c9597ec..3922a71 100644 --- a/hy/core/language.hy +++ b/hy/core/language.hy @@ -245,7 +245,7 @@ (defn last [coll] "Return last item from `coll`" - (nth coll (- (len coll) 1))) + (get (list coll) -1)) (defn list* [hd &rest tl] "Return a dotted list construed from the elements of the argument" diff --git a/tests/native_tests/core.hy b/tests/native_tests/core.hy index 00c1359..e52fd48 100644 --- a/tests/native_tests/core.hy +++ b/tests/native_tests/core.hy @@ -386,6 +386,8 @@ "NATIVE: testing the last function" (assert-equal (last [1 2 3 4]) 4) (assert-equal (last [5]) 5)) + (import itertools) + (assert-equal (last (take 5 (itertools.count 10))) 14) (defn test-neg [] "NATIVE: testing the neg? function"