Optimize -cycle a bit

* dash.el (-cycle): Use append for faster list copying that
continues to work with non-list sequences.  Fix docstring.
* dev/examples.el (-cycle): Test that returned list is a copy.
* README.md:
* dash.info:
* dash.texi: Regenerate docs.
master
Basil L. Contovounesios 5 years ago
parent f2c0e0d3c1
commit 0517ab1ed1
No known key found for this signature in database
GPG Key ID: 205AB54A5D5D8CFF
  1. 5
      README.md
  2. 8
      dash.el
  3. 187
      dash.info
  4. 5
      dash.texi
  5. 3
      dev/examples.el

@ -1809,8 +1809,9 @@ See also: [`-zip`](#-zip-rest-lists)
#### -cycle `(list)` #### -cycle `(list)`
Return an infinite copy of `list` that will cycle through the Return an infinite circular copy of `list`.
elements and repeat from the beginning. The returned list cycles through the elements of `list` and repeats
from the beginning.
```el ```el
(-take 5 (-cycle '(1 2 3))) ;; => '(1 2 3 1 2) (-take 5 (-cycle '(1 2 3))) ;; => '(1 2 3 1 2)

@ -1323,10 +1323,12 @@ See also: `-zip'"
(apply '-zip lists)) (apply '-zip lists))
(defun -cycle (list) (defun -cycle (list)
"Return an infinite copy of LIST that will cycle through the "Return an infinite circular copy of LIST.
elements and repeat from the beginning." The returned list cycles through the elements of LIST and repeats
from the beginning."
(declare (pure t) (side-effect-free t)) (declare (pure t) (side-effect-free t))
(let ((newlist (-map 'identity list))) ;; Also works with sequences that aren't lists.
(let ((newlist (append list ())))
(nconc newlist newlist))) (nconc newlist newlist)))
(defun -pad (fill-value &rest lists) (defun -pad (fill-value &rest lists)

@ -1714,8 +1714,9 @@ Other list functions not fit to be classified elsewhere.
⇒ '((1 . 3) (2 . 4)) ⇒ '((1 . 3) (2 . 4))
-- Function: -cycle (list) -- Function: -cycle (list)
Return an infinite copy of LIST that will cycle through the Return an infinite circular copy of LIST. The returned list
elements and repeat from the beginning. cycles through the elements of LIST and repeats from the
beginning.
(-take 5 (-cycle '(1 2 3))) (-take 5 (-cycle '(1 2 3)))
⇒ '(1 2 3 1 2) ⇒ '(1 2 3 1 2)
@ -3014,7 +3015,7 @@ Index
(line 56) (line 56)
* -as->: Threading macros. (line 47) * -as->: Threading macros. (line 47)
* -butlast: Other list operations. * -butlast: Other list operations.
(line 340) (line 341)
* -clone: Tree operations. (line 123) * -clone: Tree operations. (line 123)
* -common-prefix: Reductions. (line 224) * -common-prefix: Reductions. (line 224)
* -common-suffix: Reductions. (line 234) * -common-suffix: Reductions. (line 234)
@ -3047,17 +3048,17 @@ Index
* -elem-index: Indexing. (line 9) * -elem-index: Indexing. (line 9)
* -elem-indices: Indexing. (line 21) * -elem-indices: Indexing. (line 21)
* -fifth-item: Other list operations. * -fifth-item: Other list operations.
(line 320) (line 321)
* -filter: Sublist selection. (line 8) * -filter: Sublist selection. (line 8)
* -find-index: Indexing. (line 32) * -find-index: Indexing. (line 32)
* -find-indices: Indexing. (line 60) * -find-indices: Indexing. (line 60)
* -find-last-index: Indexing. (line 46) * -find-last-index: Indexing. (line 46)
* -first: Other list operations. * -first: Other list operations.
(line 234) (line 235)
* -first-item: Other list operations. * -first-item: Other list operations.
(line 271) (line 272)
* -fix: Other list operations. * -fix: Other list operations.
(line 376) (line 377)
* -fixfn: Function combinators. * -fixfn: Function combinators.
(line 177) (line 177)
* -flatten: List to list. (line 33) * -flatten: List to list. (line 33)
@ -3065,7 +3066,7 @@ Index
* -flip: Function combinators. * -flip: Function combinators.
(line 81) (line 81)
* -fourth-item: Other list operations. * -fourth-item: Other list operations.
(line 310) (line 311)
* -grade-down: Indexing. (line 81) * -grade-down: Indexing. (line 81)
* -grade-up: Indexing. (line 71) * -grade-up: Indexing. (line 71)
* -group-by: Partitioning. (line 187) * -group-by: Partitioning. (line 187)
@ -3089,13 +3090,13 @@ Index
* -keep: List to list. (line 8) * -keep: List to list. (line 8)
* -lambda: Binding. (line 253) * -lambda: Binding. (line 253)
* -last: Other list operations. * -last: Other list operations.
(line 261) (line 262)
* -last-item: Other list operations. * -last-item: Other list operations.
(line 330) (line 331)
* -let: Binding. (line 66) * -let: Binding. (line 66)
* -let*: Binding. (line 233) * -let*: Binding. (line 233)
* -list: Other list operations. * -list: Other list operations.
(line 363) (line 364)
* -map: Maps. (line 10) * -map: Maps. (line 10)
* -map-first: Maps. (line 38) * -map-first: Maps. (line 38)
* -map-indexed: Maps. (line 66) * -map-indexed: Maps. (line 66)
@ -3116,7 +3117,7 @@ Index
* -orfn: Function combinators. * -orfn: Function combinators.
(line 128) (line 128)
* -pad: Other list operations. * -pad: Other list operations.
(line 179) (line 180)
* -partial: Function combinators. * -partial: Function combinators.
(line 9) (line 9)
* -partition: Partitioning. (line 74) * -partition: Partitioning. (line 74)
@ -3162,7 +3163,7 @@ Index
* -running-sum: Reductions. (line 170) * -running-sum: Reductions. (line 170)
* -same-items?: Predicates. (line 72) * -same-items?: Predicates. (line 72)
* -second-item: Other list operations. * -second-item: Other list operations.
(line 286) (line 287)
* -select-by-indices: Sublist selection. (line 169) * -select-by-indices: Sublist selection. (line 169)
* -select-column: Sublist selection. (line 199) * -select-column: Sublist selection. (line 199)
* -select-columns: Sublist selection. (line 180) * -select-columns: Sublist selection. (line 180)
@ -3172,12 +3173,12 @@ Index
* -snoc: Other list operations. * -snoc: Other list operations.
(line 44) (line 44)
* -some: Other list operations. * -some: Other list operations.
(line 248) (line 249)
* -some-->: Threading macros. (line 84) * -some-->: Threading macros. (line 84)
* -some->: Threading macros. (line 60) * -some->: Threading macros. (line 60)
* -some->>: Threading macros. (line 72) * -some->>: Threading macros. (line 72)
* -sort: Other list operations. * -sort: Other list operations.
(line 350) (line 351)
* -splice: Maps. (line 91) * -splice: Maps. (line 91)
* -splice-list: Maps. (line 111) * -splice-list: Maps. (line 111)
* -split-at: Partitioning. (line 8) * -split-at: Partitioning. (line 8)
@ -3186,15 +3187,15 @@ Index
* -split-with: Partitioning. (line 17) * -split-with: Partitioning. (line 17)
* -sum: Reductions. (line 160) * -sum: Reductions. (line 160)
* -table: Other list operations. * -table: Other list operations.
(line 190) (line 191)
* -table-flat: Other list operations. * -table-flat: Other list operations.
(line 209) (line 210)
* -tails: Reductions. (line 214) * -tails: Reductions. (line 214)
* -take: Sublist selection. (line 102) * -take: Sublist selection. (line 102)
* -take-last: Sublist selection. (line 113) * -take-last: Sublist selection. (line 113)
* -take-while: Sublist selection. (line 147) * -take-while: Sublist selection. (line 147)
* -third-item: Other list operations. * -third-item: Other list operations.
(line 298) (line 299)
* -tree-map: Tree operations. (line 28) * -tree-map: Tree operations. (line 28)
* -tree-map-nodes: Tree operations. (line 39) * -tree-map-nodes: Tree operations. (line 39)
* -tree-mapreduce: Tree operations. (line 85) * -tree-mapreduce: Tree operations. (line 85)
@ -3347,81 +3348,81 @@ Ref: -zip-lists54162
Ref: -zip-fill54863 Ref: -zip-fill54863
Ref: -unzip55186 Ref: -unzip55186
Ref: -cycle55931 Ref: -cycle55931
Ref: -pad56304 Ref: -pad56338
Ref: -table56627 Ref: -table56661
Ref: -table-flat57417 Ref: -table-flat57451
Ref: -first58426 Ref: -first58460
Ref: -some58798 Ref: -some58832
Ref: -last59107 Ref: -last59141
Ref: -first-item59441 Ref: -first-item59475
Ref: -second-item59857 Ref: -second-item59891
Ref: -third-item60137 Ref: -third-item60171
Ref: -fourth-item60415 Ref: -fourth-item60449
Ref: -fifth-item60681 Ref: -fifth-item60715
Ref: -last-item60943 Ref: -last-item60977
Ref: -butlast61235 Ref: -butlast61269
Ref: -sort61482 Ref: -sort61516
Ref: -list61970 Ref: -list62004
Ref: -fix62301 Ref: -fix62335
Node: Tree operations62841 Node: Tree operations62875
Ref: -tree-seq63037 Ref: -tree-seq63071
Ref: -tree-map63895 Ref: -tree-map63929
Ref: -tree-map-nodes64338 Ref: -tree-map-nodes64372
Ref: -tree-reduce65193 Ref: -tree-reduce65227
Ref: -tree-reduce-from66075 Ref: -tree-reduce-from66109
Ref: -tree-mapreduce66676 Ref: -tree-mapreduce66710
Ref: -tree-mapreduce-from67536 Ref: -tree-mapreduce-from67570
Ref: -clone68822 Ref: -clone68856
Node: Threading macros69150 Node: Threading macros69184
Ref: ->69295 Ref: ->69329
Ref: ->>69787 Ref: ->>69821
Ref: -->70292 Ref: -->70326
Ref: -as->70853 Ref: -as->70887
Ref: -some->71308 Ref: -some->71342
Ref: -some->>71682 Ref: -some->>71716
Ref: -some-->72118 Ref: -some-->72152
Node: Binding72589 Node: Binding72623
Ref: -when-let72801 Ref: -when-let72835
Ref: -when-let*73286 Ref: -when-let*73320
Ref: -if-let73814 Ref: -if-let73848
Ref: -if-let*74209 Ref: -if-let*74243
Ref: -let74826 Ref: -let74860
Ref: -let*80914 Ref: -let*80948
Ref: -lambda81855 Ref: -lambda81889
Ref: -setq82657 Ref: -setq82691
Node: Side-effects83473 Node: Side-effects83507
Ref: -each83667 Ref: -each83701
Ref: -each-while84074 Ref: -each-while84108
Ref: -each-indexed84434 Ref: -each-indexed84468
Ref: -each-r84952 Ref: -each-r84986
Ref: -each-r-while85385 Ref: -each-r-while85419
Ref: -dotimes85760 Ref: -dotimes85794
Ref: -doto86063 Ref: -doto86097
Ref: --doto86490 Ref: --doto86524
Node: Destructive operations86765 Node: Destructive operations86799
Ref: !cons86938 Ref: !cons86972
Ref: !cdr87144 Ref: !cdr87178
Node: Function combinators87339 Node: Function combinators87373
Ref: -partial87613 Ref: -partial87647
Ref: -rpartial88006 Ref: -rpartial88040
Ref: -juxt88408 Ref: -juxt88442
Ref: -compose88840 Ref: -compose88874
Ref: -applify89398 Ref: -applify89432
Ref: -on89829 Ref: -on89863
Ref: -flip90355 Ref: -flip90389
Ref: -const90667 Ref: -const90701
Ref: -cut91011 Ref: -cut91045
Ref: -not91497 Ref: -not91531
Ref: -orfn91807 Ref: -orfn91841
Ref: -andfn92241 Ref: -andfn92275
Ref: -iteratefn92736 Ref: -iteratefn92770
Ref: -fixfn93439 Ref: -fixfn93473
Ref: -prodfn95008 Ref: -prodfn95042
Node: Development96077 Node: Development96111
Node: Contribute96426 Node: Contribute96460
Node: Changes97174 Node: Changes97208
Node: Contributors100173 Node: Contributors100207
Node: Index101797 Node: Index101831
 
End Tag Table End Tag Table

@ -2731,8 +2731,9 @@ See also: @code{-zip} (@pxref{-zip})
@anchor{-cycle} @anchor{-cycle}
@defun -cycle (list) @defun -cycle (list)
Return an infinite copy of @var{list} that will cycle through the Return an infinite circular copy of @var{list}.
elements and repeat from the beginning. The returned list cycles through the elements of @var{list} and repeats
from the beginning.
@example @example
@group @group

@ -782,7 +782,8 @@ new list."
(-take 7 (-cycle '(1 "and" 3))) => '(1 "and" 3 1 "and" 3 1) (-take 7 (-cycle '(1 "and" 3))) => '(1 "and" 3 1 "and" 3 1)
(-zip (-cycle '(1 2 3)) '(1 2)) => '((1 . 1) (2 . 2)) (-zip (-cycle '(1 2 3)) '(1 2)) => '((1 . 1) (2 . 2))
(-zip-with 'cons (-cycle '(1 2 3)) '(1 2)) => '((1 . 1) (2 . 2)) (-zip-with 'cons (-cycle '(1 2 3)) '(1 2)) => '((1 . 1) (2 . 2))
(-map (-partial '-take 5) (-split-at 5 (-cycle '(1 2 3)))) => '((1 2 3 1 2) (3 1 2 3 1))) (-map (-partial '-take 5) (-split-at 5 (-cycle '(1 2 3)))) => '((1 2 3 1 2) (3 1 2 3 1))
(let ((l (list 1))) (eq l (-cycle l))) => nil)
(defexamples -pad (defexamples -pad
(-pad 0 '()) => '(()) (-pad 0 '()) => '(())

Loading…
Cancel
Save