forked from magnars/dash.el
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdash.el
3531 lines (2914 loc) · 121 KB
/
dash.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; dash.el --- A modern list library for Emacs -*- lexical-binding: t -*-
;; Copyright (C) 2012-2021 Free Software Foundation, Inc.
;; Author: Magnar Sveen <magnars@gmail.com>
;; Version: 2.19.1
;; Package-Requires: ((emacs "24"))
;; Keywords: extensions, lisp
;; Homepage: https://github.com/magnars/dash.el
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; A modern list API for Emacs.
;;
;; See its overview at https://github.com/magnars/dash.el#functions.
;;; Code:
;; TODO: `gv' was introduced in Emacs 24.3, so remove this and all
;; calls to `defsetf' when support for earlier versions is dropped.
(eval-when-compile
(unless (fboundp 'gv-define-setter)
(require 'cl)))
(defgroup dash ()
"Customize group for Dash, a modern list library."
:group 'extensions
:group 'lisp
:prefix "dash-")
(defmacro !cons (car cdr)
"Destructive: Set CDR to the cons of CAR and CDR."
(declare (debug (form symbolp)))
`(setq ,cdr (cons ,car ,cdr)))
(defmacro !cdr (list)
"Destructive: Set LIST to the cdr of LIST."
(declare (debug (symbolp)))
`(setq ,list (cdr ,list)))
(defmacro --each (list &rest body)
"Evaluate BODY for each element of LIST and return nil.
Each element of LIST in turn is bound to `it' and its index
within LIST to `it-index' before evaluating BODY.
This is the anaphoric counterpart to `-each'."
(declare (debug (form body)) (indent 1))
(let ((l (make-symbol "list"))
(i (make-symbol "i")))
`(let ((,l ,list)
(,i 0)
it it-index)
(ignore it it-index)
(while ,l
(setq it (pop ,l) it-index ,i ,i (1+ ,i))
,@body))))
(defun -each (list fn)
"Call FN on each element of LIST.
Return nil; this function is intended for side effects.
Its anaphoric counterpart is `--each'.
For access to the current element's index in LIST, see
`-each-indexed'."
(declare (indent 1))
(ignore (mapc fn list)))
(defalias '--each-indexed '--each)
(defun -each-indexed (list fn)
"Call FN on each index and element of LIST.
For each ITEM at INDEX in LIST, call (funcall FN INDEX ITEM).
Return nil; this function is intended for side effects.
See also: `-map-indexed'."
(declare (indent 1))
(--each list (funcall fn it-index it)))
(defmacro --each-while (list pred &rest body)
"Evaluate BODY for each item in LIST, while PRED evaluates to non-nil.
Each element of LIST in turn is bound to `it' and its index
within LIST to `it-index' before evaluating PRED or BODY. Once
an element is reached for which PRED evaluates to nil, no further
BODY is evaluated. The return value is always nil.
This is the anaphoric counterpart to `-each-while'."
(declare (debug (form form body)) (indent 2))
(let ((l (make-symbol "list"))
(i (make-symbol "i"))
(elt (make-symbol "elt")))
`(let ((,l ,list)
(,i 0)
,elt it it-index)
(ignore it it-index)
(while (and ,l (setq ,elt (pop ,l) it ,elt it-index ,i) ,pred)
(setq it ,elt it-index ,i ,i (1+ ,i))
,@body))))
(defun -each-while (list pred fn)
"Call FN on each ITEM in LIST, while (PRED ITEM) is non-nil.
Once an ITEM is reached for which PRED returns nil, FN is no
longer called. Return nil; this function is intended for side
effects.
Its anaphoric counterpart is `--each-while'."
(declare (indent 2))
(--each-while list (funcall pred it) (funcall fn it)))
(defmacro --each-r (list &rest body)
"Evaluate BODY for each element of LIST in reversed order.
Each element of LIST in turn, starting at its end, is bound to
`it' and its index within LIST to `it-index' before evaluating
BODY. The return value is always nil.
This is the anaphoric counterpart to `-each-r'."
(declare (debug (form body)) (indent 1))
(let ((v (make-symbol "vector"))
(i (make-symbol "i")))
;; Implementation note: building a vector is considerably faster
;; than building a reversed list (vector takes less memory, so
;; there is less GC), plus `length' comes naturally. In-place
;; `nreverse' would be faster still, but BODY would be able to see
;; that, even if the modification was undone before we return.
`(let* ((,v (vconcat ,list))
(,i (length ,v))
it it-index)
(ignore it it-index)
(while (> ,i 0)
(setq ,i (1- ,i) it-index ,i it (aref ,v ,i))
,@body))))
(defun -each-r (list fn)
"Call FN on each element of LIST in reversed order.
Return nil; this function is intended for side effects.
Its anaphoric counterpart is `--each-r'."
(--each-r list (funcall fn it)))
(defmacro --each-r-while (list pred &rest body)
"Eval BODY for each item in reversed LIST, while PRED evals to non-nil.
Each element of LIST in turn, starting at its end, is bound to
`it' and its index within LIST to `it-index' before evaluating
PRED or BODY. Once an element is reached for which PRED
evaluates to nil, no further BODY is evaluated. The return value
is always nil.
This is the anaphoric counterpart to `-each-r-while'."
(declare (debug (form form body)) (indent 2))
(let ((v (make-symbol "vector"))
(i (make-symbol "i"))
(elt (make-symbol "elt")))
`(let* ((,v (vconcat ,list))
(,i (length ,v))
,elt it it-index)
(ignore it it-index)
(while (when (> ,i 0)
(setq ,i (1- ,i) it-index ,i)
(setq ,elt (aref ,v ,i) it ,elt)
,pred)
(setq it-index ,i it ,elt)
,@body))))
(defun -each-r-while (list pred fn)
"Call FN on each ITEM in reversed LIST, while (PRED ITEM) is non-nil.
Once an ITEM is reached for which PRED returns nil, FN is no
longer called. Return nil; this function is intended for side
effects.
Its anaphoric counterpart is `--each-r-while'."
(--each-r-while list (funcall pred it) (funcall fn it)))
(defmacro --dotimes (num &rest body)
"Evaluate BODY NUM times, presumably for side effects.
BODY is evaluated with the local variable `it' temporarily bound
to successive integers running from 0, inclusive, to NUM,
exclusive. BODY is not evaluated if NUM is less than 1.
This is the anaphoric counterpart to `-dotimes'."
(declare (debug (form body)) (indent 1))
(let ((n (make-symbol "num"))
(i (make-symbol "i")))
`(let ((,n ,num)
(,i 0)
it)
(ignore it)
(while (< ,i ,n)
(setq it ,i ,i (1+ ,i))
,@body))))
(defun -dotimes (num fn)
"Call FN NUM times, presumably for side effects.
FN is called with a single argument on successive integers
running from 0, inclusive, to NUM, exclusive. FN is not called
if NUM is less than 1.
This function's anaphoric counterpart is `--dotimes'."
(declare (indent 1))
(--dotimes num (funcall fn it)))
(defun -map (fn list)
"Apply FN to each item in LIST and return the list of results.
This function's anaphoric counterpart is `--map'."
(mapcar fn list))
(defmacro --map (form list)
"Eval FORM for each item in LIST and return the list of results.
Each element of LIST in turn is bound to `it' before evaluating
FORM.
This is the anaphoric counterpart to `-map'."
(declare (debug (def-form form)))
`(mapcar (lambda (it) (ignore it) ,form) ,list))
(defmacro --reduce-from (form init list)
"Accumulate a value by evaluating FORM across LIST.
This macro is like `--each' (which see), but it additionally
provides an accumulator variable `acc' which it successively
binds to the result of evaluating FORM for the current LIST
element before processing the next element. For the first
element, `acc' is initialized with the result of evaluating INIT.
The return value is the resulting value of `acc'. If LIST is
empty, FORM is not evaluated, and the return value is the result
of INIT.
This is the anaphoric counterpart to `-reduce-from'."
(declare (debug (form form form)))
`(let ((acc ,init))
(--each ,list (setq acc ,form))
acc))
(defun -reduce-from (fn init list)
"Reduce the function FN across LIST, starting with INIT.
Return the result of applying FN to INIT and the first element of
LIST, then applying FN to that result and the second element,
etc. If LIST is empty, return INIT without calling FN.
This function's anaphoric counterpart is `--reduce-from'.
For other folds, see also `-reduce' and `-reduce-r'."
(--reduce-from (funcall fn acc it) init list))
(defmacro --reduce (form list)
"Accumulate a value by evaluating FORM across LIST.
This macro is like `--reduce-from' (which see), except the first
element of LIST is taken as INIT. Thus if LIST contains a single
item, it is returned without evaluating FORM. If LIST is empty,
FORM is evaluated with `it' and `acc' bound to nil.
This is the anaphoric counterpart to `-reduce'."
(declare (debug (form form)))
(let ((lv (make-symbol "list-value")))
`(let ((,lv ,list))
(if ,lv
(--reduce-from ,form (car ,lv) (cdr ,lv))
;; Explicit nil binding pacifies lexical "variable left uninitialized"
;; warning. See issue #377 and upstream https://bugs.gnu.org/47080.
(let ((acc nil) (it nil))
(ignore acc it)
,form)))))
(defun -reduce (fn list)
"Reduce the function FN across LIST.
Return the result of applying FN to the first two elements of
LIST, then applying FN to that result and the third element, etc.
If LIST contains a single element, return it without calling FN.
If LIST is empty, return the result of calling FN with no
arguments.
This function's anaphoric counterpart is `--reduce'.
For other folds, see also `-reduce-from' and `-reduce-r'."
(if list
(-reduce-from fn (car list) (cdr list))
(funcall fn)))
(defmacro --reduce-r-from (form init list)
"Accumulate a value by evaluating FORM across LIST in reverse.
This macro is like `--reduce-from', except it starts from the end
of LIST.
This is the anaphoric counterpart to `-reduce-r-from'."
(declare (debug (form form form)))
`(let ((acc ,init))
(--each-r ,list (setq acc ,form))
acc))
(defun -reduce-r-from (fn init list)
"Reduce the function FN across LIST in reverse, starting with INIT.
Return the result of applying FN to the last element of LIST and
INIT, then applying FN to the second-to-last element and the
previous result of FN, etc. That is, the first argument of FN is
the current element, and its second argument the accumulated
value. If LIST is empty, return INIT without calling FN.
This function is like `-reduce-from' but the operation associates
from the right rather than left. In other words, it starts from
the end of LIST and flips the arguments to FN. Conceptually, it
is like replacing the conses in LIST with applications of FN, and
its last link with INIT, and evaluating the resulting expression.
This function's anaphoric counterpart is `--reduce-r-from'.
For other folds, see also `-reduce-r' and `-reduce'."
(--reduce-r-from (funcall fn it acc) init list))
(defmacro --reduce-r (form list)
"Accumulate a value by evaluating FORM across LIST in reverse order.
This macro is like `--reduce', except it starts from the end of
LIST.
This is the anaphoric counterpart to `-reduce-r'."
(declare (debug (form form)))
`(--reduce ,form (reverse ,list)))
(defun -reduce-r (fn list)
"Reduce the function FN across LIST in reverse.
Return the result of applying FN to the last two elements of
LIST, then applying FN to the third-to-last element and the
previous result of FN, etc. That is, the first argument of FN is
the current element, and its second argument the accumulated
value. If LIST contains a single element, return it without
calling FN. If LIST is empty, return the result of calling FN
with no arguments.
This function is like `-reduce' but the operation associates from
the right rather than left. In other words, it starts from the
end of LIST and flips the arguments to FN. Conceptually, it is
like replacing the conses in LIST with applications of FN,
ignoring its last link, and evaluating the resulting expression.
This function's anaphoric counterpart is `--reduce-r'.
For other folds, see also `-reduce-r-from' and `-reduce'."
(if list
(--reduce-r (funcall fn it acc) list)
(funcall fn)))
(defmacro --reductions-from (form init list)
"Return a list of FORM's intermediate reductions across LIST.
That is, a list of the intermediate values of the accumulator
when `--reduce-from' (which see) is called with the same
arguments.
This is the anaphoric counterpart to `-reductions-from'."
(declare (debug (form form form)))
`(nreverse
(--reduce-from (cons (let ((acc (car acc))) (ignore acc) ,form) acc)
(list ,init)
,list)))
(defun -reductions-from (fn init list)
"Return a list of FN's intermediate reductions across LIST.
That is, a list of the intermediate values of the accumulator
when `-reduce-from' (which see) is called with the same
arguments.
This function's anaphoric counterpart is `--reductions-from'.
For other folds, see also `-reductions' and `-reductions-r'."
(--reductions-from (funcall fn acc it) init list))
(defmacro --reductions (form list)
"Return a list of FORM's intermediate reductions across LIST.
That is, a list of the intermediate values of the accumulator
when `--reduce' (which see) is called with the same arguments.
This is the anaphoric counterpart to `-reductions'."
(declare (debug (form form)))
(let ((lv (make-symbol "list-value")))
`(let ((,lv ,list))
(if ,lv
(--reductions-from ,form (car ,lv) (cdr ,lv))
(let (acc it)
(ignore acc it)
(list ,form))))))
(defun -reductions (fn list)
"Return a list of FN's intermediate reductions across LIST.
That is, a list of the intermediate values of the accumulator
when `-reduce' (which see) is called with the same arguments.
This function's anaphoric counterpart is `--reductions'.
For other folds, see also `-reductions' and `-reductions-r'."
(if list
(--reductions-from (funcall fn acc it) (car list) (cdr list))
(list (funcall fn))))
(defmacro --reductions-r-from (form init list)
"Return a list of FORM's intermediate reductions across reversed LIST.
That is, a list of the intermediate values of the accumulator
when `--reduce-r-from' (which see) is called with the same
arguments.
This is the anaphoric counterpart to `-reductions-r-from'."
(declare (debug (form form form)))
`(--reduce-r-from (cons (let ((acc (car acc))) (ignore acc) ,form) acc)
(list ,init)
,list))
(defun -reductions-r-from (fn init list)
"Return a list of FN's intermediate reductions across reversed LIST.
That is, a list of the intermediate values of the accumulator
when `-reduce-r-from' (which see) is called with the same
arguments.
This function's anaphoric counterpart is `--reductions-r-from'.
For other folds, see also `-reductions' and `-reductions-r'."
(--reductions-r-from (funcall fn it acc) init list))
(defmacro --reductions-r (form list)
"Return a list of FORM's intermediate reductions across reversed LIST.
That is, a list of the intermediate values of the accumulator
when `--reduce-re' (which see) is called with the same arguments.
This is the anaphoric counterpart to `-reductions-r'."
(declare (debug (form list)))
(let ((lv (make-symbol "list-value")))
`(let ((,lv (reverse ,list)))
(if ,lv
(--reduce-from (cons (let ((acc (car acc))) (ignore acc) ,form) acc)
(list (car ,lv))
(cdr ,lv))
;; Explicit nil binding pacifies lexical "variable left uninitialized"
;; warning. See issue #377 and upstream https://bugs.gnu.org/47080.
(let ((acc nil) (it nil))
(ignore acc it)
(list ,form))))))
(defun -reductions-r (fn list)
"Return a list of FN's intermediate reductions across reversed LIST.
That is, a list of the intermediate values of the accumulator
when `-reduce-r' (which see) is called with the same arguments.
This function's anaphoric counterpart is `--reductions-r'.
For other folds, see also `-reductions-r-from' and
`-reductions'."
(if list
(--reductions-r (funcall fn it acc) list)
(list (funcall fn))))
(defmacro --filter (form list)
"Return a new list of the items in LIST for which FORM evals to non-nil.
Each element of LIST in turn is bound to `it' and its index
within LIST to `it-index' before evaluating FORM.
This is the anaphoric counterpart to `-filter'.
For the opposite operation, see also `--remove'."
(declare (debug (form form)))
(let ((r (make-symbol "result")))
`(let (,r)
(--each ,list (when ,form (push it ,r)))
(nreverse ,r))))
(defun -filter (pred list)
"Return a new list of the items in LIST for which PRED returns non-nil.
Alias: `-select'.
This function's anaphoric counterpart is `--filter'.
For similar operations, see also `-keep' and `-remove'."
(--filter (funcall pred it) list))
(defalias '-select '-filter)
(defalias '--select '--filter)
(defmacro --remove (form list)
"Return a new list of the items in LIST for which FORM evals to nil.
Each element of LIST in turn is bound to `it' and its index
within LIST to `it-index' before evaluating FORM.
This is the anaphoric counterpart to `-remove'.
For the opposite operation, see also `--filter'."
(declare (debug (form form)))
`(--filter (not ,form) ,list))
(defun -remove (pred list)
"Return a new list of the items in LIST for which PRED returns nil.
Alias: `-reject'.
This function's anaphoric counterpart is `--remove'.
For similar operations, see also `-keep' and `-filter'."
(--remove (funcall pred it) list))
(defalias '-reject '-remove)
(defalias '--reject '--remove)
(defmacro --remove-first (form list)
"Remove the first item from LIST for which FORM evals to non-nil.
Each element of LIST in turn is bound to `it' and its index
within LIST to `it-index' before evaluating FORM. This is a
non-destructive operation, but only the front of LIST leading up
to the removed item is a copy; the rest is LIST's original tail.
If no item is removed, then the result is a complete copy.
This is the anaphoric counterpart to `-remove-first'."
(declare (debug (form form)))
(let ((front (make-symbol "front"))
(tail (make-symbol "tail")))
`(let ((,tail ,list) ,front)
(--each-while ,tail (not ,form)
(push (pop ,tail) ,front))
(if ,tail
(nconc (nreverse ,front) (cdr ,tail))
(nreverse ,front)))))
(defun -remove-first (pred list)
"Remove the first item from LIST for which PRED returns non-nil.
This is a non-destructive operation, but only the front of LIST
leading up to the removed item is a copy; the rest is LIST's
original tail. If no item is removed, then the result is a
complete copy.
Alias: `-reject-first'.
This function's anaphoric counterpart is `--remove-first'.
See also `-map-first', `-remove-item', and `-remove-last'."
(--remove-first (funcall pred it) list))
(defalias '-reject-first '-remove-first)
(defalias '--reject-first '--remove-first)
(defmacro --remove-last (form list)
"Remove the last item from LIST for which FORM evals to non-nil.
Each element of LIST in turn is bound to `it' before evaluating
FORM. The result is a copy of LIST regardless of whether an
element is removed.
This is the anaphoric counterpart to `-remove-last'."
(declare (debug (form form)))
`(nreverse (--remove-first ,form (reverse ,list))))
(defun -remove-last (pred list)
"Remove the last item from LIST for which PRED returns non-nil.
The result is a copy of LIST regardless of whether an element is
removed.
Alias: `-reject-last'.
This function's anaphoric counterpart is `--remove-last'.
See also `-map-last', `-remove-item', and `-remove-first'."
(--remove-last (funcall pred it) list))
(defalias '-reject-last '-remove-last)
(defalias '--reject-last '--remove-last)
(defalias '-remove-item #'remove
"Return a copy of LIST with all occurrences of ITEM removed.
The comparison is done with `equal'.
\n(fn ITEM LIST)")
(defmacro --keep (form list)
"Eval FORM for each item in LIST and return the non-nil results.
Like `--filter', but returns the non-nil results of FORM instead
of the corresponding elements of LIST. Each element of LIST in
turn is bound to `it' and its index within LIST to `it-index'
before evaluating FORM.
This is the anaphoric counterpart to `-keep'."
(declare (debug (form form)))
(let ((r (make-symbol "result"))
(m (make-symbol "mapped")))
`(let (,r)
(--each ,list (let ((,m ,form)) (when ,m (push ,m ,r))))
(nreverse ,r))))
(defun -keep (fn list)
"Return a new list of the non-nil results of applying FN to each item in LIST.
Like `-filter', but returns the non-nil results of FN instead of
the corresponding elements of LIST.
Its anaphoric counterpart is `--keep'."
(--keep (funcall fn it) list))
(defun -non-nil (list)
"Return a copy of LIST with all nil items removed."
(declare (pure t) (side-effect-free t))
(--filter it list))
(defmacro --map-indexed (form list)
"Eval FORM for each item in LIST and return the list of results.
Each element of LIST in turn is bound to `it' and its index
within LIST to `it-index' before evaluating FORM. This is like
`--map', but additionally makes `it-index' available to FORM.
This is the anaphoric counterpart to `-map-indexed'."
(declare (debug (form form)))
(let ((r (make-symbol "result")))
`(let (,r)
(--each ,list
(push ,form ,r))
(nreverse ,r))))
(defun -map-indexed (fn list)
"Apply FN to each index and item in LIST and return the list of results.
This is like `-map', but FN takes two arguments: the index of the
current element within LIST, and the element itself.
This function's anaphoric counterpart is `--map-indexed'.
For a side-effecting variant, see also `-each-indexed'."
(--map-indexed (funcall fn it-index it) list))
(defmacro --map-when (pred rep list)
"Anaphoric form of `-map-when'."
(declare (debug (form form form)))
(let ((r (make-symbol "result")))
`(let (,r)
(--each ,list (!cons (if ,pred ,rep it) ,r))
(nreverse ,r))))
(defun -map-when (pred rep list)
"Return a new list where the elements in LIST that do not match the PRED function
are unchanged, and where the elements in LIST that do match the PRED function are mapped
through the REP function.
Alias: `-replace-where'
See also: `-update-at'"
(--map-when (funcall pred it) (funcall rep it) list))
(defalias '-replace-where '-map-when)
(defalias '--replace-where '--map-when)
(defun -map-first (pred rep list)
"Replace first item in LIST satisfying PRED with result of REP called on this item.
See also: `-map-when', `-replace-first'"
(let (front)
(while (and list (not (funcall pred (car list))))
(push (car list) front)
(!cdr list))
(if list
(-concat (nreverse front) (cons (funcall rep (car list)) (cdr list)))
(nreverse front))))
(defmacro --map-first (pred rep list)
"Anaphoric form of `-map-first'."
(declare (debug (def-form def-form form)))
`(-map-first (lambda (it) ,pred) (lambda (it) (ignore it) ,rep) ,list))
(defun -map-last (pred rep list)
"Replace last item in LIST satisfying PRED with result of REP called on this item.
See also: `-map-when', `-replace-last'"
(nreverse (-map-first pred rep (reverse list))))
(defmacro --map-last (pred rep list)
"Anaphoric form of `-map-last'."
(declare (debug (def-form def-form form)))
`(-map-last (lambda (it) ,pred) (lambda (it) (ignore it) ,rep) ,list))
(defun -replace (old new list)
"Replace all OLD items in LIST with NEW.
Elements are compared using `equal'.
See also: `-replace-at'"
(declare (pure t) (side-effect-free t))
(--map-when (equal it old) new list))
(defun -replace-first (old new list)
"Replace the first occurrence of OLD with NEW in LIST.
Elements are compared using `equal'.
See also: `-map-first'"
(declare (pure t) (side-effect-free t))
(--map-first (equal old it) new list))
(defun -replace-last (old new list)
"Replace the last occurrence of OLD with NEW in LIST.
Elements are compared using `equal'.
See also: `-map-last'"
(declare (pure t) (side-effect-free t))
(--map-last (equal old it) new list))
(defmacro --mapcat (form list)
"Anaphoric form of `-mapcat'."
(declare (debug (form form)))
`(apply 'append (--map ,form ,list)))
(defun -mapcat (fn list)
"Return the concatenation of the result of mapping FN over LIST.
Thus function FN should return a list."
(--mapcat (funcall fn it) list))
(defmacro --iterate (form init n)
"Anaphoric version of `-iterate'."
(declare (debug (form form form)))
(let ((res (make-symbol "result"))
(len (make-symbol "n")))
`(let ((,len ,n))
(when (> ,len 0)
(let* ((it ,init)
(,res (list it)))
(dotimes (_ (1- ,len))
(push (setq it ,form) ,res))
(nreverse ,res))))))
(defun -iterate (fun init n)
"Return a list of iterated applications of FUN to INIT.
This means a list of the form:
(INIT (FUN INIT) (FUN (FUN INIT)) ...)
N is the length of the returned list."
(--iterate (funcall fun it) init n))
(defun -flatten (l)
"Take a nested list L and return its contents as a single, flat list.
Note that because `nil' represents a list of zero elements (an
empty list), any mention of nil in L will disappear after
flattening. If you need to preserve nils, consider `-flatten-n'
or map them to some unique symbol and then map them back.
Conses of two atoms are considered \"terminals\", that is, they
aren't flattened further.
See also: `-flatten-n'"
(declare (pure t) (side-effect-free t))
(if (and (listp l) (listp (cdr l)))
(-mapcat '-flatten l)
(list l)))
(defun -flatten-n (num list)
"Flatten NUM levels of a nested LIST.
See also: `-flatten'"
(declare (pure t) (side-effect-free t))
(dotimes (_ num)
(setq list (apply #'append (mapcar #'-list list))))
list)
(defun -concat (&rest lists)
"Return a new list with the concatenation of the elements in the supplied LISTS."
(declare (pure t) (side-effect-free t))
(apply 'append lists))
(defalias '-copy 'copy-sequence
"Create a shallow copy of LIST.
\(fn LIST)")
(defun -splice (pred fun list)
"Splice lists generated by FUN in place of elements matching PRED in LIST.
FUN takes the element matching PRED as input.
This function can be used as replacement for `,@' in case you
need to splice several lists at marked positions (for example
with keywords).
See also: `-splice-list', `-insert-at'"
(let (r)
(--each list
(if (funcall pred it)
(let ((new (funcall fun it)))
(--each new (!cons it r)))
(!cons it r)))
(nreverse r)))
(defmacro --splice (pred form list)
"Anaphoric form of `-splice'."
(declare (debug (def-form def-form form)))
`(-splice (lambda (it) ,pred) (lambda (it) ,form) ,list))
(defun -splice-list (pred new-list list)
"Splice NEW-LIST in place of elements matching PRED in LIST.
See also: `-splice', `-insert-at'"
(-splice pred (lambda (_) new-list) list))
(defmacro --splice-list (pred new-list list)
"Anaphoric form of `-splice-list'."
(declare (debug (def-form form form)))
`(-splice-list (lambda (it) ,pred) ,new-list ,list))
(defun -cons* (&rest args)
"Make a new list from the elements of ARGS.
The last 2 elements of ARGS are used as the final cons of the
result, so if the final element of ARGS is not a list, the result
is a dotted list. With no ARGS, return nil."
(declare (pure t) (side-effect-free t))
(let* ((len (length args))
(tail (nthcdr (- len 2) args))
(last (cdr tail)))
(if (null last)
(car args)
(setcdr tail (car last))
args)))
(defun -snoc (list elem &rest elements)
"Append ELEM to the end of the list.
This is like `cons', but operates on the end of list.
If ELEMENTS is non nil, append these to the list as well."
(-concat list (list elem) elements))
(defmacro --first (form list)
"Return the first item in LIST for which FORM evals to non-nil.
Return nil if no such element is found.
Each element of LIST in turn is bound to `it' and its index
within LIST to `it-index' before evaluating FORM.
This is the anaphoric counterpart to `-first'."
(declare (debug (form form)))
(let ((n (make-symbol "needle")))
`(let (,n)
(--each-while ,list (or (not ,form)
(ignore (setq ,n it))))
,n)))
(defun -first (pred list)
"Return the first item in LIST for which PRED returns non-nil.
Return nil if no such element is found.
To get the first item in the list no questions asked, use `car'.
Alias: `-find'.
This function's anaphoric counterpart is `--first'."
(--first (funcall pred it) list))
(defalias '-find '-first)
(defalias '--find '--first)
(defmacro --some (form list)
"Return non-nil if FORM evals to non-nil for at least one item in LIST.
If so, return the first such result of FORM.
Each element of LIST in turn is bound to `it' and its index
within LIST to `it-index' before evaluating FORM.
This is the anaphoric counterpart to `-some'."
(declare (debug (form form)))
(let ((n (make-symbol "needle")))
`(let (,n)
(--each-while ,list (not (setq ,n ,form)))
,n)))
(defun -some (pred list)
"Return (PRED x) for the first LIST item where (PRED x) is non-nil, else nil.
Alias: `-any'.
This function's anaphoric counterpart is `--some'."
(--some (funcall pred it) list))
(defalias '-any '-some)
(defalias '--any '--some)
(defmacro --every (form list)
"Return non-nil if FORM evals to non-nil for all items in LIST.
If so, return the last such result of FORM. Otherwise, once an
item is reached for which FORM yields nil, return nil without
evaluating FORM for any further LIST elements.
Each element of LIST in turn is bound to `it' and its index
within LIST to `it-index' before evaluating FORM.
This macro is like `--every-p', but on success returns the last
non-nil result of FORM instead of just t.
This is the anaphoric counterpart to `-every'."
(declare (debug (form form)))
(let ((a (make-symbol "all")))
`(let ((,a t))
(--each-while ,list (setq ,a ,form))
,a)))
(defun -every (pred list)
"Return non-nil if PRED returns non-nil for all items in LIST.
If so, return the last such result of PRED. Otherwise, once an
item is reached for which PRED returns nil, return nil without
calling PRED on any further LIST elements.
This function is like `-every-p', but on success returns the last
non-nil result of PRED instead of just t.
This function's anaphoric counterpart is `--every'."
(--every (funcall pred it) list))
(defmacro --last (form list)
"Anaphoric form of `-last'."
(declare (debug (form form)))
(let ((n (make-symbol "needle")))
`(let (,n)
(--each ,list
(when ,form (setq ,n it)))
,n)))
(defun -last (pred list)
"Return the last x in LIST where (PRED x) is non-nil, else nil."
(--last (funcall pred it) list))
(defalias '-first-item 'car
"Return the first item of LIST, or nil on an empty list.
See also: `-second-item', `-last-item'.
\(fn LIST)")
;; Ensure that calls to `-first-item' are compiled to a single opcode,
;; just like `car'.
(put '-first-item 'byte-opcode 'byte-car)
(put '-first-item 'byte-compile 'byte-compile-one-arg)
(defalias '-second-item 'cadr
"Return the second item of LIST, or nil if LIST is too short.
See also: `-third-item'.
\(fn LIST)")
(defalias '-third-item
(if (fboundp 'caddr)
#'caddr
(lambda (list) (car (cddr list))))
"Return the third item of LIST, or nil if LIST is too short.
See also: `-fourth-item'.
\(fn LIST)")
(defun -fourth-item (list)
"Return the fourth item of LIST, or nil if LIST is too short.
See also: `-fifth-item'."
(declare (pure t) (side-effect-free t))
(car (cdr (cdr (cdr list)))))
(defun -fifth-item (list)
"Return the fifth item of LIST, or nil if LIST is too short.
See also: `-last-item'."
(declare (pure t) (side-effect-free t))
(car (cdr (cdr (cdr (cdr list))))))
(defun -last-item (list)
"Return the last item of LIST, or nil on an empty list."
(declare (pure t) (side-effect-free t))
(car (last list)))
;; Use `with-no-warnings' to suppress unbound `-last-item' or
;; undefined `gv--defsetter' warnings arising from both
;; `gv-define-setter' and `defsetf' in certain Emacs versions.
(with-no-warnings
(if (fboundp 'gv-define-setter)
(gv-define-setter -last-item (val x) `(setcar (last ,x) ,val))
(defsetf -last-item (x) (val) `(setcar (last ,x) ,val))))
(defun -butlast (list)
"Return a list of all items in list except for the last."
;; no alias as we don't want magic optional argument
(declare (pure t) (side-effect-free t))
(butlast list))
(defmacro --count (pred list)
"Anaphoric form of `-count'."
(declare (debug (form form)))
(let ((r (make-symbol "result")))
`(let ((,r 0))
(--each ,list (when ,pred (setq ,r (1+ ,r))))
,r)))
(defun -count (pred list)
"Counts the number of items in LIST where (PRED item) is non-nil."
(--count (funcall pred it) list))
(defun ---truthy? (obj)
"Return OBJ as a boolean value (t or nil)."
(declare (pure t) (side-effect-free t))
(and obj t))
(defmacro --any? (form list)
"Anaphoric form of `-any?'."
(declare (debug (form form)))
`(and (--some ,form ,list) t))
(defun -any? (pred list)
"Return t if (PRED x) is non-nil for any x in LIST, else nil.
Alias: `-any-p', `-some?', `-some-p'"
(--any? (funcall pred it) list))
(defalias '-some? '-any?)
(defalias '--some? '--any?)
(defalias '-any-p '-any?)
(defalias '--any-p '--any?)
(defalias '-some-p '-any?)
(defalias '--some-p '--any?)
(defmacro --all? (form list)
"Return t if FORM evals to non-nil for all items in LIST.
Otherwise, once an item is reached for which FORM yields nil,
return nil without evaluating FORM for any further LIST elements.