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
|
" list.vim
" @Author: Tom Link (micathom AT gmail com?subject=[vim])
" @Website: http://www.vim.org/account/profile.php?user_id=4037
" @License: GPL (see http://www.gnu.org/licenses/gpl.txt)
" @Created: 2007-06-30.
" @Last Change: 2016-03-22.
" @Revision: 68
""" List related functions {{{1
" For the following functions please see ../../test/tlib.vim for examples.
" :def: function! tlib#list#Inject(list, initial_value, funcref)
" EXAMPLES: >
" echo tlib#list#Inject([1,2,3], 0, function('Add')
" => 6
function! tlib#list#Inject(list, value, Function) "{{{3
if empty(a:list)
return a:value
else
let item = a:list[0]
let rest = a:list[1:-1]
let value = call(a:Function, [a:value, item])
return tlib#list#Inject(rest, value, a:Function)
endif
endf
" EXAMPLES: >
" tlib#list#Compact([0,1,2,3,[], {}, ""])
" => [1,2,3]
function! tlib#list#Compact(list) "{{{3
return filter(copy(a:list), '!empty(v:val)')
endf
" EXAMPLES: >
" tlib#list#Flatten([0,[1,2,[3,""]]])
" => [0,1,2,3,""]
function! tlib#list#Flatten(list) "{{{3
let acc = []
for e in a:list
if type(e) == 3
let acc += tlib#list#Flatten(e)
else
call add(acc, e)
endif
unlet e
endfor
return acc
endf
" :def: function! tlib#list#FindAll(list, filter, ?process_expr="")
" Basically the same as filter()
"
" EXAMPLES: >
" tlib#list#FindAll([1,2,3], 'v:val >= 2')
" => [2, 3]
function! tlib#list#FindAll(list, filter, ...) "{{{3
let rv = filter(copy(a:list), a:filter)
if a:0 >= 1 && a:1 != ''
let rv = map(rv, a:1)
endif
return rv
endf
" :def: function! tlib#list#Find(list, filter, ?default="", ?process_expr="")
"
" EXAMPLES: >
" tlib#list#Find([1,2,3], 'v:val >= 2')
" => 2
function! tlib#list#Find(list, filter, ...) "{{{3
let default = a:0 >= 1 ? a:1 : ''
let expr = a:0 >= 2 ? a:2 : ''
return get(tlib#list#FindAll(a:list, a:filter, expr), 0, default)
endf
" EXAMPLES: >
" tlib#list#Any([1,2,3], 'v:val >= 2')
" => 1
function! tlib#list#Any(list, expr) "{{{3
return !empty(tlib#list#FindAll(a:list, a:expr))
endf
" EXAMPLES: >
" tlib#list#All([1,2,3], 'v:val >= 2')
" => 0
function! tlib#list#All(list, expr) "{{{3
return len(tlib#list#FindAll(a:list, a:expr)) == len(a:list)
endf
" EXAMPLES: >
" tlib#list#Remove([1,2,1,2], 2)
" => [1,1,2]
function! tlib#list#Remove(list, element) "{{{3
let idx = index(a:list, a:element)
if idx != -1
call remove(a:list, idx)
endif
return a:list
endf
" EXAMPLES: >
" tlib#list#RemoveAll([1,2,1,2], 2)
" => [1,1]
function! tlib#list#RemoveAll(list, element) "{{{3
call filter(a:list, 'v:val != a:element')
return a:list
endf
" :def: function! tlib#list#Zip(lists, ?default='')
" EXAMPLES: >
" tlib#list#Zip([[1,2,3], [4,5,6]])
" => [[1,4], [2,5], [3,6]]
function! tlib#list#Zip(lists, ...) "{{{3
TVarArg 'default'
let lists = copy(a:lists)
let max = 0
for l in lists
let ll = len(l)
if ll > max
let max = ll
endif
endfor
" TLogVAR default, max
return map(range(0, max - 1), 's:GetNthElement(v:val, lists, default)')
endf
function! s:GetNthElement(n, lists, default) "{{{3
" TLogVAR a:n, a:lists, a:default
return map(copy(a:lists), 'get(v:val, a:n, a:default)')
endf
function! tlib#list#Uniq(list, ...) "{{{3
" TLogVAR a:list
TVarArg ['get_value', ''], ['remove_empty', 0]
if remove_empty
call filter(a:list, 'type(v:val) == 0 || !empty(v:val)')
endif
" CREDITS: Based on syntastic#util#unique(list) by scrooloose
let emptystring = 0
let seen = {}
let uniques = []
if empty(get_value)
for e in a:list
if e == ''
if !emptystring
let emptystring = 1
call add(uniques, e)
endif
elseif !has_key(seen, e)
let seen[e] = 1
call add(uniques, e)
endif
unlet e
endfor
else
for e in a:list
let v = eval(printf(get_value, string(e)))
if v == ''
if !emptystring
let emptystring = 1
call add(uniques, v)
endif
elseif !has_key(seen, v)
let seen[v] = 1
call add(uniques, v)
endif
unlet e v
endfor
endif
return uniques
endf
function! tlib#list#ToDictionary(list, default, ...) "{{{3
TVarArg ['generator', '']
let dict = {}
for item in a:list
if !empty(item)
let dict[item] = empty(generator) ? a:default : call(generator, [item, a:default])
endif
endfor
return dict
endf
|