Merge pull request #782 from acron0/last-core

Added (last) function to core language
This commit is contained in:
Berker Peksag 2015-04-08 09:20:59 +03:00
commit 4938163ec4
4 changed files with 25 additions and 1 deletions

View File

@ -61,3 +61,4 @@
* Shenyang Zhao <dev@zsy.im> * Shenyang Zhao <dev@zsy.im>
* Zack M. Davis <code@zackmdavis.net> * Zack M. Davis <code@zackmdavis.net>
* Adrià Garriga Alonso <adria@monkingme.com> * Adrià Garriga Alonso <adria@monkingme.com>
* Antony Woods <antony@teamwoods.org>

View File

@ -945,6 +945,18 @@ This can be confirmed via Python's built-in ``help`` function::
Multiplies input by three and returns result Multiplies input by three and returns result
(END) (END)
last
-----------
.. versionadded:: 0.10.2
``last`` can be used for accessing the last element of a collection:
.. code-block:: clj
=> (last [2 4 6])
6
let let
--- ---

View File

@ -243,6 +243,10 @@
"Return true if x is an iterator" "Return true if x is an iterator"
(isinstance x collections.Iterator)) (isinstance x collections.Iterator))
(defn last [coll]
"Return last item from `coll`"
(get (list coll) -1))
(defn list* [hd &rest tl] (defn list* [hd &rest tl]
"Return a dotted list construed from the elements of the argument" "Return a dotted list construed from the elements of the argument"
(if (not tl) (if (not tl)
@ -416,7 +420,7 @@
dec distinct disassemble drop drop-last drop-while empty? even? dec distinct disassemble drop drop-last drop-while empty? even?
every? first filter filterfalse flatten float? gensym identity every? first filter filterfalse flatten float? gensym identity
inc input instance? integer integer? integer-char? interleave 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? macroexpand macroexpand-1 map merge-with name neg? nil? none?
nth numeric? odd? pos? range read remove repeat repeatedly nth numeric? odd? pos? range read remove repeat repeatedly
rest reduce second some string string? symbol? take take-nth rest reduce second some string string? symbol? take take-nth

View File

@ -382,6 +382,13 @@
;; should not work for an int ;; should not work for an int
(assert-false (iterator? 5))) (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))
(import itertools)
(assert-equal (last (take 5 (itertools.count 10))) 14)
(defn test-neg [] (defn test-neg []
"NATIVE: testing the neg? function" "NATIVE: testing the neg? function"
(assert-true (neg? -2)) (assert-true (neg? -2))