Reimplement butlast in terms of drop-last

This commit is contained in:
han semaj 2014-08-22 21:51:12 +12:00
parent 7f5c8e39d8
commit 23f31d4ac1
3 changed files with 39 additions and 1 deletions

View File

@ -6,6 +6,31 @@ Hy Core
Core Functions
===============
.. _butlast-fn:
butlast
-------
Usage: ``(butlast coll)``
Returns an iterator of all but the last item in ``coll``.
.. code-block:: hy
=> (list (butlast (range 10)))
[0, 1, 2, 3, 4, 5, 6, 7, 8]
=> (list (butlast [1]))
[]
=> (list (butlast []))
[]
=> (import itertools)
=> (list (take 5 (butlast (itertools.count 10))))
[10, 11, 12, 13, 14]
.. _is-coll-fn:
coll?

View File

@ -38,7 +38,7 @@
(defn butlast [coll]
"Returns coll except of last element."
(itertools.islice coll 0 (dec (len coll))))
(drop-last 1 coll))
(defn coll? [coll]
"Checks whether item is a collection"

View File

@ -38,6 +38,19 @@
(assert-false (coll? "abc"))
(assert-false (coll? 1)))
(defn test-butlast []
"NATIVE: testing butlast function"
(assert-equal (list (butlast (range 10)))
[0 1 2 3 4 5 6 7 8])
(assert-equal (list (butlast [1]))
[])
(assert-equal (list (butlast []))
[])
; with an infinite sequence
(import itertools)
(assert-equal (list (take 5 (butlast (itertools.count 10))))
[10 11 12 13 14]))
(defn test-cycle []
"NATIVE: testing cycle"
(assert-equal (list (cycle [])) [])