-
Notifications
You must be signed in to change notification settings - Fork 64
/
List.txt
1010 lines (813 loc) · 28.1 KB
/
List.txt
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
*vital/Data/List.txt* list utilities library.
Maintainer: ujihisa <ujihisa at gmail com>
==============================================================================
CONTENTS *Vital.Data.List-contents*
INTRODUCTION |Vital.Data.List-introduction|
TERM |Vital.Data.List.term|
INTERFACE |Vital.Data.List-interface|
Functions |Vital.Data.List-functions|
==============================================================================
INTRODUCTION *Vital.Data.List-introduction*
*Vital.Data.List* is a list utilities library. It provides some functions to
manipulate |List|.
>
let s:V = vital#{plugin-name}#new()
let s:L = s:V.import("Data.List")
echo s:L.cons(1, [2, 3])
" [1, 2, 3]
echo s:L.conj([2, 3], 1)
" [2, 3, 1]
echo s:L.foldl({ memo, val -> memo + val }, 0, range(1, 10))
" 55 := 1+2+3+4+5+6+7+8+9+10
echo s:L.count({ x -> x % 2 == 0 }, [1, 2, 3, 4, 5])
"=> 2
echo s:L.intersect(['a', 'b', 'c'], ['b', 'c'])
" ['b', 'c']
s:L.new(3, { i -> i * 2 })
"=> [0, 2, 4]
echo s:L.permutations([1, 2, 3])
" [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
<
==============================================================================
TERM *Vital.Data.List-term*
{function} *Vital.Data.List-term-function*
It's just |Funcref|, but also |String| as expression works fine for
backward compatibility. |String| for this is DEPRECATED.
For new code please always simply use Vim's |expr-lambda| notation for
this.
==============================================================================
INTERFACE *Vital.Data.List-interface*
------------------------------------------------------------------------------
FUNCTIONS *Vital.Data.List-functions*
new({size}, {f}) *Vital.Data.List.new()*
Creates a new |List| with given arguments. The given |Funcref| {f} is
called for {size} times with index.
Note that's vital Data.List does not provide a new wrapper list
dictionary or whatever. It simply uses Vim's |Lists|.
>
s:L.new(3, { i -> i * 2 })
"=> [0, 2, 4]
s:L.new(4, { -> 'hello' })
"=> ['hello', 'hello', 'hello', 'hello']
<
Basically this function is equivalent to the following one line.
>
new(size, f) == map(range(a:size), a:f)
<
pop({list}) *Vital.Data.List.pop()*
Removes the last element from |List| {list} and returns the element,
as if the {list} is a stack.
Destructive. This modifies {list}.
push({list}, {val}) *Vital.Data.List.push()*
Appends {val} to the end of |List| {list} and returns the list itself,
as if the {list} is a stack.
Destructive. This modifies {list}.
shift({list}) *Vital.Data.List.shift()*
Removes the first element from |List| {list} and returns the element.
Destructive. This modifies {list}.
unshift({list}, {val}) *Vital.Data.List.unshift()*
Inserts {val} to the head of |List| {list} and returns the list
itself.
Destructive. This modifies {list}.
cons({val}, {list}) *Vital.Data.List.cons()*
Makes new |List| which first item is {val} and the rest of items are
|List| {list}.
See also: |Vital.Data.List.conj()|
>
echo s:L.cons(1, [2, 3])
" [1, 2, 3]
echo s:L.cons(1, [])
" [1]
echo s:L.cons([1], [2, 3])
" [[1], 2, 3]
echo s:L.cons([1], 2)
" ERROR: E745
<
Non-destructive. This does not modify {list}.
uncons({list}) *Vital.Data.List.uncons()*
Returns a pair of a head element and tail elements.
{list} must be nonempty, otherwise it throws an error.
>
echo s:L.uncons([1, 2, 3, 4, 5])
" [1, [2, 3, 4, 5]]
echo s:L.uncons([1])
" [1, []]
echo s:L.uncons([])
" ERROR: vital: Data.List: ...
<
Non-destructive. This does not modify {list}.
conj({list}, {val}) *Vital.Data.List.conj()*
Makes new |List| which first items are |List| {list} and the final
item is {val}.
See also: |Vital.Data.List.cons()|
>
echo s:L.conj([2, 3], 1)
" [2, 3, 1]
echo s:L.conj([], 1)
" [1]
echo s:L.conj([2, 3], [1])
" [2, 3, [1]]
echo s:L.conj(2, [1])
" ERROR: E745
<
Non-destructive. This does not modify {list}.
map({list}, {function}) *Vital.Data.List.map()*
Use this if you'd like to keep the original list. Vim's built-in
|map()| destroys the given {list}, but this doesn't.
Generalized map(). The followings are different of |map()|:
* Don't require taking the index as the argument
(See the section of 'If {expr2} is a Funcref...' in |map()|)
* Don't require copying
(See the section of 'The operation is done in-place' in |map()|)
* Remove v:key support
* Don't modify {list} itself.
>
function! Succ(x) abort
return a:x + 1
endfunction
echo s:L.map(range(0, 4), { x + 1 })
" [1, 2, 3, 4, 5]
echo s:L.map(range(0, 4), function('Succ'))
" [1, 2, 3, 4, 5]
echo s:L.map(range(0, 4), 'v:val + 1') " DEPRECATED
" [1, 2, 3, 4, 5]
<
But this maybe slower than builtin |map()|.
Non-destructive. This does not modify {list}.
filter({list}, {function}) *Vital.Data.List.filter()*
Use this if you'd like to keep the original list. Vim's built-in
|filter()| destroys the given {list}, but this doesn't.
Generalized filter(). The followings are different of |filter()|:
* Don't require taking the index as the argument
(See the section of 'If {expr2} is a Funcref...' in |filter()|)
* Don't require copying
(See the section of 'The operation is done in-place' in |filter()|)
* Remove v:key support
* Don't modify {list} itself.
>
function! Even(x) abort
return a:x % 2 is 0
endfunction
let xs = range(0, 9)
echo s:L.filter(xs, function('Even'))
" [0, 2, 4, 6, 8]
echo s:L.filter(xs, 'v:val % 2 is 0')
" [0, 2, 4, 6, 8]
<
But this maybe slower than builtin |filter()|.
Non-destructive. This does not modify {list}.
uniq({list}) *Vital.Data.List.uniq()*
Removes duplicate elements from |List| {list}, nondestructively. In
particular, it keeps only the first occurrence of each element.
See also: |Vital.Data.List.uniq_by()|
>
uniq(['vim', 'emacs', 'vim', 'vim']) == ['vim', 'emacs']
<
Non-destructive. This does not modify {list}.
uniq_by({list}, {function}) *Vital.Data.List.uniq_by()*
Removes duplicate elements from |List| {list}, nondestructively. In
particular, it keeps only the first occurrence of each element. The
uniqueness is judged with the value {function} to which a formula is
applied.
See also: |Vital.Data.List.uniq()|
>
uniq_by(
\ ['vim', 'Vim', 'VIM', 'emacs', 'Emacs', 'EMACS', 'gVim', 'GVIM'],
\ 'tolower(v:val)') == ['vim', 'emacs', 'gVim']
<
Non-destructive. This does not modify {list}.
clear({list}) *Vital.Data.List.clear()*
Removes all the items of |List| {list}. Returns the empty list.
Destructive. This modifies {list}.
concat({list}) *Vital.Data.List.concat()*
Concatenates |List| {list} of lists.
>
echo s:L.concat([[1], [2, 3]])
" [1, 2, 3]
<
This is similar to |Vital.Data.List.flatten()| but this doesn't
flatten recursively.
Non-destructive. This does not modify {list}.
flatten({list} [, {limit}]) *Vital.Data.List.flatten()*
Take each {list} elements in |List| {list} into a new {list}
recursively. When the {limit} argument is given, the function keeps
nested items by the {limit} is maximum size.
>
echo s:L.flatten([[1], [2, 3]])
" [1, 2, 3]
echo s:L.flatten([[1], 2, 3])
" [1, 2, 3]
echo s:L.flatten([[['a']], [[['b']], 'c']], 2)
" ['a', ['b'], 'c']
<
Non-destructive. This does not modify {list}.
sort({list}, {function}) *Vital.Data.List.sort()*
Sorts the items in |List| {list} in-place. Returns {list}. When
{function} is a |Funcref|, this function returns the same result as
|sort()|. When {function} is a |String| expression, this function uses
{function} to compare items. Inside {function} a:a and a:b have the
value of the current items. The evaluating result of {function} must
have zero if they are equal, 1 or bigger if a:a sorts after the a:b,
-1 or smaller if a:a sorts before a:b.
>
function! MyCompare(i1, i2)
return a:i1 ==
\ a:i2 ? 0 :
\ a:i1 > a:i2 ? 1 :
\ -1
endfunction
let list = ['pineapple', 'orange', 'banana', 'apple']
echo s:L.sort(copy(list), function('MyCompare'))
" ['apple', 'banana', 'orange', 'pineapple']
echo s:L.sort([3, 1, 2], 'a:a - a:b')
" [1, 2, 3]
echo s:L.sort(copy(list), 'len(a:a)-len(a:b)')
" ['apple', 'orange', 'banana', 'pineapple']
<
Notice:
If you use {function} as |String| expression, this function gives up
job safety (thread safety). It may not work correctly. Please use
lambda expression or partial applying of function if it can be used.
Destructive. This modifies {list}.
sort_by({list}, {function}) *Vital.Data.List.sort_by()*
Returns a sorted |List| with key in |List| {list}.
>
function! Lookup(x)
return a:x.field
endfunction
let list = [{'field': 'pineapple'}, {'field': 'orange'}, {'field': 'banana'}, {'field': 'apple'}]
echo s:L.sort_by(copy(list), 'v:val.field')
" [{'field': 'apple'}, {'field': 'banana'}, {'field': 'orange'}, {'field': 'pineapple'}]
echo s:L.sort_by(copy(list), function('Lookup'))
" [{'field': 'apple'}, {'field': 'banana'}, {'field': 'orange'}, {'field': 'pineapple'}]
<
Non-destructive. This does not modify {list}.
max_by({list}, {function}) *Vital.Data.List.max_by()*
Returns a maximum value in {list} through given {function}.
Returns 0 if {list} is empty.
"v:val" can be used in {function} if {function} is string expression.
>
echo s:L.max_by(
\ ['pineapple', 'orange', 'banana', 'apple'],
\ 'len(v:val)')
" pineapple
echo s:L.max_by([20, -50, -15, 30], function('abs'))
" -50
<
Non-destructive. This does not modify {list}.
min_by({list}, {function}) *Vital.Data.List.min_by()*
Returns a minimum value in |List| {list} through given {function}.
Returns 0 if {list} is empty.
"v:val" can be used in {function} if {function} is string expression.
>
echo s:L.min_by(
\ ['pineapple', 'orange', 'banana', 'apple'],
\ 'len(v:val)')
" apple
echo s:L.min_by([20, -50, -15, 30], function('abs'))
" -15
<
Non-destructive. This does not modify {list}.
char_range({from}, {to}) *Vital.Data.List.char_range()*
Returns a |List| of letters from {from} to {to}.
has({list}, {value}) *Vital.Data.List.has()*
Returns Number 1 if {value} is in |List| {list}, otherwise zero.
Non-destructive. This does not modify {list}.
has_index({list}, {index}) *Vital.Data.List.has_index()*
Returns Number 1 if can point to {index} for |List| {list}, otherwise
zero. If {index} is negative Number, this function returns zero.
Non-destructive. This does not modify {list}.
span({function}, {list}) *Vital.Data.List.span()*
Returns a list of two lists where concatenation of them is
equal to {list}, all the items of the first list satisfy {function} and
the first item of the second list does not satisfy {function}.
If {function} is the string expression, |v:val| has the value of the
current item.
>
function! Under5(x) abort
return a:x < 5
endfunction
echo s:L.span('v:val < 5', [1, 3, 5, 2])
" [[1, 3], [5, 2]]
echo s:L.span(function('Under5'), [1, 3, 5, 2])
" [[1, 3], [5, 2]]
echo s:L.span('v:val==1', [1, 2])
" [[1], [2]]
echo s:L.span('v:val > 3', [1, 2, 3, 4, 5])
" [[], [1, 2, 3, 4, 5]]
echo s:L.span('v:val < 3', [1, 2, 3, 4, 5])
" [[1, 2], [3, 4, 5]]
<
If you know Haskell, this span() is like Haskell's Data.List.span just
for your info.
Non-destructive. This does not modify {list}.
break({function}, {list}) *Vital.Data.List.break()*
Returns a list of two lists where concatenation of them is
equal to {list}, all the items of the first list do not satisfy
{function} and the first item of the second list satisfies {function}.
If {function} is the string expression, |v:val| has the value of the
current item.
>
function! Is5(x) abort
return a:x < 5
endfunction
echo s:L.break('v:val == 5', [1, 3, 5, 2])
" [[1, 3], [5, 2]]
echo s:L.break(function('Is5'), [1, 3, 5, 2])
" [[1, 3], [5, 2]]
echo s:L.break("v:val==1", [1, 2])
" [[], [1, 2]]
echo s:L.break('v:val > 3', [1, 2, 3, 4, 5])
" [[1, 2, 3], [4, 5]]
echo s:L.break('v:val < 3', [1, 2, 3, 4, 5])
" [[], [1, 2, 3, 4, 5]]
<
If you know Haskell, this break() is like Haskell's Data.List.break
just for your info.
Non-destructive. This does not modify {list}.
take_while({function}, {list}) *Vital.Data.List.take_while()*
Returns a list which is from the beginning of the given {list} to an
element that all of them satisfies given expression {function}.
If {function} is the string expression, |v:val| has the value of the
current item.
>
function! Under5(x) abort
return a:x < 5
endfunction
echo s:L.take_while('v:val < 5', [1, 3, 5, 2])
" [1, 3]
echo s:L.take_while(function('Under5'), [1, 3, 5, 2])
" [1, 3]
echo s:L.take_while('v:val == 1', [1, 2])
" [1]
echo s:L.take_while('v:val > 3', [1, 2, 3, 4, 5])
" []
echo s:L.take_while('v:val < 3', [1, 2, 3, 4, 5])
" [1, 2]
<
If you know Haskell, this take_while() is like Haskell's
Data.List.takeWhile just for your info.
Non-destructive. This does not modify {list}.
drop_while({function}, {list}) *Vital.Data.List.drop_while()*
Returns the suffix remaining after |Vital.Data.List.take_while()|.
If {function} is the string expression, |v:val| has the value of the
current item.
>
function! Under5(x) abort
return a:x < 5
endfunction
echo s:L.drop_while('v:val < 5', [1, 3, 5, 2])
" [5, 2]
echo s:L.drop_while(function('Under5'), [1, 3, 5, 2])
" [5, 2]
echo s:L.drop_while("v:val==1", [1, 2])
" [2]
echo s:L.drop_while('v:val > 3', [1, 2, 3, 4, 5])
" [1, 2, 3, 4, 5]
echo s:L.drop_while('v:val < 3', [1, 2, 3, 4, 5])
" [3, 4, 5]
<
If you know Haskell, this drop_while() is like Haskell's
Data.List.dropWhile just for your info.
Non-destructive. This does not modify {list}.
all({function}, {list}) *Vital.Data.List.all()*
Returns Number 1 if all the items in |List| {list} fulfill the
condition {function}, zero otherwise.
If {list} is empty, this function returns 1.
>
function! Even(x) abort
return a:x % 2 == 0
endfunction
echo s:L.all('v:val % 2 == 0', [2, 8, 4, 6])
" 1
echo s:L.all(function('Even'), [2, 8, 4, 6])
" 1
echo s:L.all('v:val % 2 == 1', [2, 8, 4, 6])
" 0
echo s:L.all('v:val % 2 == 0', [2, 8, 5, 6])
" 0
echo s:L.all('0 < v:val', [2, 8, 4, 6])
" 1
echo s:L.all('0 < v:val', [2, 0, 4, 6])
" 0
<
If you know Haskell, this all() is like Haskell's Prelude.all just for
your info.
Non-destructive. This does not modify {list}.
any({function}, {list}) *Vital.Data.List.any()*
Returns Number 1 if at least one item in |List| {list} fulfills the
condition {function}, zero otherwise. If {list} is empty, this
function returns 0.
>
function! Even(x) abort
return a:x % 2 == 0
endfunction
echo s:L.any('v:val % 2 == 0', [2, 8, 4, 6])
" 1
echo s:L.any(function('Even'), [2, 8, 4, 6])
" 1
echo s:L.any('v:val % 2 == 1', [2, 8, 4, 6])
" 0
echo s:L.any('v:val % 2 == 0', [2, 8, 5, 6])
" 1
echo s:L.any('0 < v:val', [2, 8, 4, 6])
" 1
echo s:L.any('0 < v:val', [2, 0, 4, 6])
" 1
<
If you know Haskell, this any() is like Haskell's Prelude.any just for
your info.
Non-destructive. This does not modify {list}.
and({list}) *Vital.Data.List.and()*
Returns Number 1 if all the items of |List| {list} are non-zero
Numbers, zero otherwise. If {list} is empty, this function returns 1.
>
echo s:L.and([1, 2, 3, 1])
" 1
echo s:L.and([1, 0, 3, 1])
" 0
echo s:L.and([0, 0, 0, 0])
" 0
<
If you know Haskell, this and() is like Haskell's Prelude.and just for
your info.
Non-destructive. This does not modify {list}.
or({list}) *Vital.Data.List.or()*
Returns Number 1 if at least one item in List {list} is non-zero,
zero otherwise. If {list} is empty, this function returns 0.
>
echo s:L.or([1, 2, 3, 1])
" 1
echo s:L.or([1, 0, 3, 1])
" 1
echo s:L.or([0, 0, 0, 0])
" 0
<
If you know Haskell, this or() is like Haskell's Prelude.or just for
your info.
Non-destructive. This does not modify {list}.
partition({function}, {list}) *Vital.Data.List.partition()*
Gives a {function} as predicate. Takes a tuple. The tuple's first
field is elements that satisfies the predicate. The second field is
elements that doesn't satisfy the predicate.
Behaves like Haskell's Data.List.partition().
>
function! Even(x) abort
return a:x % 2 == 0
endfunction
s:L.partition(function('Even'), range(5))
" [[0, 2, 4], [1, 3]]
s:L.partition('v:val % 2 == 0', range(5))
" [[0, 2, 4], [1, 3]]
<
Non-destructive. This does not modify {list}.
map_accum({function}, {xs}, {init}) *Vital.Data.List.map_accum()*
This is similar to |map()| but the followings are different:
* it doesn't destroy {xs}
* it holds previous accumulator
* you also have to specify initial accumulator value
* you also have to let {function} return the next accumulator value
>
function! Plus(x, y) abort
return [a:x + a:y, a:y]
endfunction
echo s:L.map_accum('[v:val + v:memo, v:memo]', [1, 2, 3], 10)
" [11, 12, 13]
echo s:L.map_accum(function('Plus'), [1, 2, 3], 10)
" [11, 12, 13]
echo s:L.map_accum('[v:val + v:memo, v:memo + 1]', [1, 2, 3], 10)
" [11, 13, 15]
<
Non-destructive. This does not modify {xs}.
foldl({function}, {init}, {xs}) *Vital.Data.List.foldl()*
Reduces the list {xs} using the binary operator {function}, from left
to right. The starting value of the reduction (typically the
left-identity of the operator) is {init}.
Behaves like Haskell's Data.List.foldl().
foldl(f, z, [x1, x2, ..., xn]) ==
f(... f(f(z, x1), x2) ..., xn)
>
function! Plus(x, y) abort
return a:x + a:y
endfunction
function! Pair(x, y) abort
return [a:x, a:y]
endfunction
echo s:L.foldl('v:memo + v:val', 0, range(1, 10))
" 55 := 1+2+3+4+5+6+7+8+9+10
echo s:L.foldl(function('Plus'), 0, range(1, 10))
" 55
echo s:L.foldl(function('Pair'), 0, [1, 2])
" [[0, 1], 2]
<
See also: foldl1, foldr, foldr1
If you know Haskell, this foldl() is like Haskell's Data.List.foldl
just for your info.
Non-destructive. This does not modify {xs}.
foldl1({function}, {xs}) *Vital.Data.List.foldl1()*
Sames |Data.List.foldl()|, but doesn't take the initial value. Takes
the first element from {xs} as the initial value.
Behaves like Haskell's Data.List.foldl1().
>
function! Plus(x, y) abort
return a:x + a:y
endfunction
function! Pair(x, y) abort
return [a:x, a:y]
endfunction
echo s:L.foldl1('v:memo + v:val', range(1, 10))
" 55
echo s:L.foldl1(function('Plus'), range(1, 10))
" 55
echo s:L.foldl1(function('Pair'), [0, 1, 2])
" [[0, 1], 2]
<
Non-destructive. This does not modify {xs}.
foldr({function}, {init}, {xs}) *Vital.Data.List.foldr()*
Reduces the list {xs} using the binary operator {function}, from right
to left. The starting value of the reduction (typically the
right-identity of the operator) is {init}.
Behaves like Haskell's Data.List.foldr().
>
function! Plus(x, y) abort
return a:x + a:y
endfunction
function! Pair(x, y) abort
return [a:x, a:y]
endfunction
echo s:L.foldr('v:val + v:memo', 0, range(1, 10))
" 55
echo s:L.foldr(function('Plus'), 0, range(1, 10))
" 55
echo s:L.foldr(function('Pair'), [], [1, 2])
" [1, [2, []]]
<
Non-destructive. This does not modify {xs}.
foldr1({function}, {xs}) *Vital.Data.List.foldr1()*
Sames |Data.List.foldr()|, but doesn't take the initial value. Takes
the last element from {xs} as the initial value.
Behaves like Haskell's Data.List.foldr1().
>
function! Plus(x, y) abort
return a:x + a:y
endfunction
function! Pair(x, y) abort
return [a:x, a:y]
endfunction
echo s:L.foldr1('v:val + v:memo', range(1, 10))
" 55
echo s:L.foldr1(function('Plus'), range(1, 10))
" 55
echo s:L.foldr1(function('Pair'), [1, 2, []])
" [1, [2, []]]
<
Non-destructive. This does not modify {xs}.
count({f}, {xs}) *Vital.Data.List.count()*
NOTE: This is different to Vim script's native |count()| function.
NOTE: This is experimental. Unlike other Data.List functions, you
can't provide a string represated pseudo function to {f}.
Returns number of items in {xs} that satisfies the given predicate
function {f}.
>
echo s:L.count({ x -> x == 2 }, [1, 2, 3, 4, 5])
"=> 1
echo s:L.count({ x -> x % 2 == 0 }, [1, 2, 3, 4, 5])
"=> 2
function! s:f(x)
return a:x % 2 == 0
endfunction
echo s:L.count(function('s:f', [1, 2, 3, 4, 5])
"=> 2
<
It scans from left to right. O(n).
Non-destructive. This does not modify {xs}.
zip(...) *Vital.Data.List.zip()*
Unifies lists in parallel. If the length of the lists is different,
adjusts for shorter list, longer list is sliced.
Behaves like python's zip().
>
echo s:L.zip([1, 2, 3], [4, 5, 6])
" [[1, 4], [2, 5], [3, 6]]
echo s:L.zip([1, 2, 3], [4, 5, 6], [7, 8, 9])
" [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
<
Non-destructive. This does not modify {xs}.
zip_fill({list}, {list}, {elem}) *Vital.Data.List.zip_fill()*
Similar to |Vital.Data.List.zip()|, but goes until the longer one.
>
echo s:L.zip_fill([1, 2, 3, 10, 20], [4, 5, 6], 100)
" [[1, 4], [2, 5], [3, 6], [10, 100], [20, 100]]
echo s:L.zip_fill([1, 2, 3], [4, 5, 6, 10, 20], 200)
" [[1, 4], [2, 5], [3, 6], [200, 10], [200, 20]]
<
Non-destructive. This does not modify {xs}.
with_index({list} [, {offset}]) *Vital.Data.List.with_index()*
Returns {list} with index. {offset} means the base of index.
If you specify {offset}, index starts with {offset}.
>
echo s:L.with_index(['a', 'b', 'c'])
" [['a', 0], ['b', 1], ['c', 2]]
echo s:L.with_index(['a', 'b', 'c'], 2)
" [['a', 2], ['b', 3], ['c', 4]]
<
This function is useful when used with |:for|.
For example, when you have lines as a list of string and you want to
output a line with a line number to each line, you may write as below.
>
for idx in range(1, len(lines))
echo idx.': '.lines[idx]
endfor
<
This procedure can be rewritten using with_index() as below.
>
for [line, idx] in s:L.with_index(lines, 1)
echo idx.': '.line
endfor
<
Non-destructive. This does not modify {xs}.
find({list}, {default}, {function}) *Vital.Data.List.find()*
Returns the first value in {list} where the given {function} is
satisfied. {default} is returned when no item satisfies {function}.
{function} must be a |String| or a |Funcref|.
>
function! MyPredicate(x)
return a:x % 2 == 0
endfunction
echo s:L.find([1, 2, 3, 1, 2, 3], '*not-found*', function('MyPredicate'))
" 2
echo s:L.find([1, 2, 3, 1, 2, 3], '*not-found*', 'v:val % 2 == 0')
" 2
echo s:L.find([1, 2, 3], '*not-found*', 'v:val % 10 == 0')
" '*not-found*'
<
If you know Haskell, this find() is like Haskell's Data.List.find
just for your info.
Non-destructive. This does not modify {xs}.
*Vital.Data.List.find_index()*
find_index({list}, {function} [, {start} [, {default}]])
Returns the lowest index in {list} where the given {function} is
satisfied.
If you specify {start}, start looking at the item with index {start}
(may be negative for an item relative to the end).
{default} is returned when no item satisfies {function}. If {default}
is omitted, -1 is used.
>
function! Odd(x) abort
return a:x % 2 == 1
endfunction
echo s:L.find_index([0, 1, 2, 3], 'v:val % 2 == 1')
" 1
echo s:L.find_index([0, 1, 2, 3], function('Odd'))
" 1
echo s:L.find_index([0, 1, 2, 3], 'v:val > 10')
" -1
echo s:L.find_index([0, 1, 2, 3], 'v:val % 2 == 1', 1)
" 2
let default_val = -10
let constant_false_expr = '0'
echo s:L.find_index([0, 1, 2, 3], constant_false_expr, 0, default_val)
" -10
<
Non-destructive. This does not modify {xs}.
*Vital.Data.List.find_last_index()*
find_last_index({list}, {function} [, {start} [, {default}]])
Similar to find_index but this returns the highest index.
Traversing is done in reverse order.
>
function! Odd(x) abort
return a:x % 2 == 1
endfunction
echo s:L.find_last_index([0, 1, 2, 3], 'v:val % 2 == 1')
" 3
echo s:L.find_last_index([0, 1, 2, 3], function('Odd'))
" 3
<
Non-destructive. This does not modify {xs}.
*Vital.Data.List.find_indices()*
find_indices({list}, {function} [, {start}])
Similar to find_index but this returns all of indices specifying
{function}.
When no indices found, empty list is returned.
>
function! Odd(x) abort
return a:x % 2 == 1
endfunction
echo s:L.find_indices([0, 1, 2, 3], 'v:val % 2 == 1')
" [1, 3]
echo s:L.find_indices([0, 1, 2, 3], function('Odd'))
" [1, 3]
echo s:L.find_indices([0, 1, 2, 3], 'v:val > 10')
" []
echo s:L.find_indices([0, 1, 2, 3], 'v:val % 2 == 1', 2)
" [3]
echo s:L.find_indices([0, 1, 2, 3], 'v:val % 2 == 1', 1)
" [1, 3]
echo s:L.find_indices([0, 1, 2, 3], 'v:val % 2 == 1', -2)
" [3]
<
Non-destructive. This does not modify {xs}.
has_common_items({list1}, {list2}) *Vital.Data.List.has_common_items()*
Returns non-zero if a:list1 and a:list2 have a common item, otherwise
zero.
>
echo s:L.has_common_items(['a', 'b', 'c'], ['b', 'c'])
" 1
echo s:L.has_common_items(['a', 'c'], ['b', 'c'])
" 1
echo s:L.has_common_items(['a'], ['b', 'c'])
" 0
<
Non-destructive. This does not modify {xs}.
intersect({list1}, {list2}) *Vital.Data.List.intersect()*
Returns a |List| of common items between {list1} and {list2}, and it's
unordered and uniquified.
>
echo s:L.intersect(['a', 'b', 'c'], ['b', 'c'])
" ['b', 'c']
echo s:L.intersect(['a', 'c'], ['b', 'c'])
" ['c']
echo s:L.intersect(['a', 'a'], ['a', 'a'])
" ['a']
echo s:L.intersect(['a'], ['b', 'c'])
" []
<
Non-destructive. This does not modify {xs}.
group_by({list}, {function}) *Vital.Data.List.group_by()*
Returns a |Dictionary| grouped by the result of {function}.
"v:val" can be used in {function} if {function} is a string expression.
>
echo s:L.group_by(['a', 'b', 'ab'], 'len(v:val)')
" {'1': ['a', 'b'], '2': ['ab']}
echo s:L.group_by(['a', 'b', 'ab'], function('len'))
" {'1': ['a', 'b'], '2': ['ab']}
echo s:L.group_by(['a', 'b', 'ab'], 'v:val[0]')
" {'a': ['a', 'ab'], 'b': ['b']}
<
Non-destructive. This does not modify {xs}.
*Vital.Data.List.binary_search()*
binary_search({list}, {target}, [{func}, [{dict}]])
Returns the index in {list} where the item has a value equal to
{target} by binary search. {list} must be sorted. If {target} is not
found, it returns -1.
When {func} is given, it is used to check the lhs of {func} is less
than the rhs of {func}. {func} is the same as |sort()| of {func}.
You can reuse {func} used for |sort()| to search with
|Vital.Data.List.binary_search|.
{dict} is used as "self" in "dict" function.
>
echo s:L.binary_search([1, 3, 5, 7], 3)
" 1
echo s:L.binary_search([1, 3, 5, 7], 2)
" -1
function! CompareWithFirstElem(a, b)
return a:a[0] < a:b[0] ? -1 : a:a[0] > a:b[0] ? 1 : 0
endfunction
echo s:L.binary_search([[1, 'd'], [3, 'c'], [5, 'b'], [7, 'a']], [3, 'c'], 'CompareWithFirstElem')
" 1
echo s:L.binary_search([[1, 'd'], [3, 'c'], [5, 'b'], [7, 'a']], [10, 'c'], 'CompareWithFirstElem')
" -1
<
You can control the condition for the search by {func}. Below example
shows the way to search a list by its length.
>
let CompareByLength = {}
function! CompareByLength.func(a, b) dict
return len(a:a) - len(a:b)
endfunction
echo s:L.binary_search(['a', 'aa', 'aaa'], 'vi', CompareByLength.func, CompareByLength)
" 1
echo s:L.binary_search(['a', 'aa', 'aaa'], 'vivi', CompareByLength.func, CompareByLength)
" -1
<
Non-destructive. This does not modify {xs}.
product({lists}) *Vital.Data.List.product()*
Returns Cartesian product of elements in the {lists}.
>
echo s:L.product([[1, 2], [4, 5]])
" [[1, 4], [1, 5], [2, 4], [2, 5]]
echo s:L.product([range(2), range(2), range(2)])
" [[0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0], [1, 1, 1]]
<
Non-destructive. This does not modify {xs}.
permutations({list} [, {r}]) *Vital.Data.List.permutations()*
Returns successive {r} length permutations of elements in the {list}.
If {r} is not specified, then {r} defaults to the length of the {list}
and all possible full-length permutations are generated.
>
echo s:L.permutations([1, 2, 3])
" [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
echo s:L.permutations([1, 2, 3], 2)
" [[1, 2] , [1, 3], [2, 1], [2, 3], [3, 1], [3, 2]]
<
Non-destructive. This does not modify {xs}.
combinations({list}, {r}) *Vital.Data.List.combinations()*
Returns successive {r} length combinations of elements in the {list}.
>