Implement drop-last
This commit is contained in:
parent
7e5befa615
commit
7f5c8e39d8
@ -822,6 +822,32 @@ Raises ``ValueError`` if ``n`` is negative.
|
|||||||
=> (list (drop 6 [1 2 3 4 5]))
|
=> (list (drop 6 [1 2 3 4 5]))
|
||||||
[]
|
[]
|
||||||
|
|
||||||
|
|
||||||
|
.. _drop-last-fn:
|
||||||
|
|
||||||
|
drop-last
|
||||||
|
---------
|
||||||
|
|
||||||
|
Usage: ``(drop-last n coll)``
|
||||||
|
|
||||||
|
Return an iterator of all but the last ``n`` items in ``coll``. Raise ``ValueError`` if ``n`` is negative.
|
||||||
|
|
||||||
|
.. code-block:: hy
|
||||||
|
|
||||||
|
=> (list (drop-last 5 (range 10 20)))
|
||||||
|
[10, 11, 12, 13, 14]
|
||||||
|
|
||||||
|
=> (list (drop-last 0 (range 5)))
|
||||||
|
[0, 1, 2, 3, 4]
|
||||||
|
|
||||||
|
=> (list (drop-last 100 (range 100)))
|
||||||
|
[]
|
||||||
|
|
||||||
|
=> (import itertools)
|
||||||
|
=> (list (take 5 (drop-last 100 (itertools.count 10))))
|
||||||
|
[10, 11, 12, 13, 14]
|
||||||
|
|
||||||
|
|
||||||
.. _drop-while-fn:
|
.. _drop-while-fn:
|
||||||
|
|
||||||
drop-while
|
drop-while
|
||||||
|
@ -57,7 +57,6 @@
|
|||||||
(and (instance? (type :foo) k)
|
(and (instance? (type :foo) k)
|
||||||
(.startswith k (get :foo 0))))
|
(.startswith k (get :foo 0))))
|
||||||
|
|
||||||
|
|
||||||
(defn dec [n]
|
(defn dec [n]
|
||||||
"Decrement n by 1"
|
"Decrement n by 1"
|
||||||
(_numeric-check n)
|
(_numeric-check n)
|
||||||
@ -119,6 +118,12 @@
|
|||||||
"Drop `count` elements from `coll` and yield back the rest"
|
"Drop `count` elements from `coll` and yield back the rest"
|
||||||
(itertools.islice coll count nil))
|
(itertools.islice coll count nil))
|
||||||
|
|
||||||
|
(defn drop-last [n coll]
|
||||||
|
"Return a sequence of all but the last n elements in coll."
|
||||||
|
(let [[iters (itertools.tee coll)]]
|
||||||
|
(map first (apply zip [(get iters 0)
|
||||||
|
(drop n (get iters 1))]))))
|
||||||
|
|
||||||
(defn empty? [coll]
|
(defn empty? [coll]
|
||||||
"Return True if `coll` is empty"
|
"Return True if `coll` is empty"
|
||||||
(= 0 (len coll)))
|
(= 0 (len coll)))
|
||||||
@ -354,7 +359,7 @@
|
|||||||
|
|
||||||
|
|
||||||
(def *exports* '[butlast calling-module-name coll? cons cons? cycle
|
(def *exports* '[butlast calling-module-name coll? cons cons? cycle
|
||||||
dec distinct disassemble drop 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? list*
|
interpose iterable? iterate iterator? keyword? list*
|
||||||
|
@ -117,6 +117,19 @@
|
|||||||
(setv res (list (take 5 (drop 2 (iterate inc 0)))))
|
(setv res (list (take 5 (drop 2 (iterate inc 0)))))
|
||||||
(assert-equal res [2 3 4 5 6]))
|
(assert-equal res [2 3 4 5 6]))
|
||||||
|
|
||||||
|
(defn test-drop-last []
|
||||||
|
"NATIVE: testing drop-last function"
|
||||||
|
(assert-equal (list (drop-last 5 (range 10 20)))
|
||||||
|
[10 11 12 13 14])
|
||||||
|
(assert-equal (list (drop-last 0 (range 5)))
|
||||||
|
[0 1 2 3 4])
|
||||||
|
(assert-equal (list (drop-last 100 (range 100)))
|
||||||
|
[])
|
||||||
|
; with an infinite sequence
|
||||||
|
(import itertools)
|
||||||
|
(assert-equal (list (take 5 (drop-last 100 (itertools.count 10))))
|
||||||
|
[10 11 12 13 14]))
|
||||||
|
|
||||||
(defn test-drop-while []
|
(defn test-drop-while []
|
||||||
"NATIVE: testing drop-while function"
|
"NATIVE: testing drop-while function"
|
||||||
(setv res (list (drop-while even? [2 4 7 8 9])))
|
(setv res (list (drop-while even? [2 4 7 8 9])))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user