From cbd942fd026b502d9221bd8f1c6169321a47a5a4 Mon Sep 17 00:00:00 2001 From: Antony Woods Date: Wed, 18 Mar 2015 15:23:43 +0000 Subject: [PATCH] Added (last) function to core language --- hy/core/language.hy | 6 +++++- tests/native_tests/core.hy | 5 +++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/hy/core/language.hy b/hy/core/language.hy index 20483ff..c9597ec 100644 --- a/hy/core/language.hy +++ b/hy/core/language.hy @@ -243,6 +243,10 @@ "Return true if x is an iterator" (isinstance x collections.Iterator)) +(defn last [coll] + "Return last item from `coll`" + (nth coll (- (len coll) 1))) + (defn list* [hd &rest tl] "Return a dotted list construed from the elements of the argument" (if (not tl) @@ -416,7 +420,7 @@ dec distinct disassemble drop drop-last drop-while empty? even? every? first filter filterfalse flatten float? gensym identity inc input instance? integer integer? integer-char? interleave - interpose iterable? iterate iterator? keyword keyword? list* + interpose iterable? iterate iterator? keyword keyword? last list* macroexpand macroexpand-1 map merge-with name neg? nil? none? nth numeric? odd? pos? range read remove repeat repeatedly rest reduce second some string string? symbol? take take-nth diff --git a/tests/native_tests/core.hy b/tests/native_tests/core.hy index de5d8c7..00c1359 100644 --- a/tests/native_tests/core.hy +++ b/tests/native_tests/core.hy @@ -382,6 +382,11 @@ ;; should not work for an int (assert-false (iterator? 5))) +(defn test-last [] + "NATIVE: testing the last function" + (assert-equal (last [1 2 3 4]) 4) + (assert-equal (last [5]) 5)) + (defn test-neg [] "NATIVE: testing the neg? function" (assert-true (neg? -2))