summaryrefslogtreecommitdiffstats
path: root/autoload/vital/_crystal/Data/List.vim
diff options
context:
space:
mode:
Diffstat (limited to 'autoload/vital/_crystal/Data/List.vim')
-rw-r--r--autoload/vital/_crystal/Data/List.vim26
1 files changed, 24 insertions, 2 deletions
diff --git a/autoload/vital/_crystal/Data/List.vim b/autoload/vital/_crystal/Data/List.vim
index 55703b3d..1aac999f 100644
--- a/autoload/vital/_crystal/Data/List.vim
+++ b/autoload/vital/_crystal/Data/List.vim
@@ -1,5 +1,14 @@
if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'crystal') == -1
+" ___vital___
+" NOTE: lines between '" ___vital___' is generated by :Vitalize.
+" Do not mofidify the code nor insert new lines before '" ___vital___'
+function! s:_SID() abort
+ return matchstr(expand('<sfile>'), '<SNR>\zs\d\+\ze__SID$')
+endfunction
+execute join(['function! vital#_crystal#Data#List#import() abort', printf("return map({'combinations': '', 'and': '', 'sort_by': '', 'foldr1': '', 'sort': '', 'flatten': '', 'has_index': '', 'find_indices': '', 'any': '', 'unshift': '', 'span': '', 'pop': '', 'binary_search': '', 'uniq_by': '', 'or': '', 'all': '', 'zip': '', 'find_last_index': '', 'find': '', 'partition': '', 'shift': '', 'permutations': '', 'break': '', 'max_by': '', 'foldl': '', 'foldr': '', 'find_index': '', 'drop_while': '', 'group_by': '', 'take_while': '', 'conj': '', 'push': '', 'char_range': '', 'cons': '', 'foldl1': '', 'intersect': '', 'concat': '', 'map_accum': '', 'clear': '', 'has_common_items': '', 'product': '', 'zip_fill': '', 'uniq': '', 'has': '', 'min_by': '', 'with_index': ''}, \"vital#_crystal#function('<SNR>%s_' . v:key)\")", s:_SID()), 'endfunction'], "\n")
+delfunction s:_SID
+" ___vital___
" Utilities for list.
let s:save_cpo = &cpo
@@ -175,6 +184,11 @@ function! s:take_while(f, xs) abort
return s:span(a:f, a:xs)[0]
endfunction
+" similar to Haskell's Data.List.dropWhile
+function! s:drop_while(f, xs) abort
+ return s:span(a:f, a:xs)[1]
+endfunction
+
" similar to Haskell's Data.List.partition
function! s:partition(f, xs) abort
return [filter(copy(a:xs), a:f), filter(copy(a:xs), '!(' . a:f . ')')]
@@ -271,14 +285,22 @@ endfunction
" similar to Ruby's detect or Haskell's find.
function! s:find(list, default, f) abort
+ let l:Call = type(a:f) is type(function('function'))
+ \ ? function('call')
+ \ : function('s:_call_string_expr')
+
for x in a:list
- if eval(substitute(a:f, 'v:val', string(x), 'g'))
+ if l:Call(a:f, [x])
return x
endif
endfor
return a:default
endfunction
+function! s:_call_string_expr(expr, args) abort
+ return map([a:args[0]], a:expr)[0]
+endfunction
+
" Returns the index of the first element which satisfies the given expr.
function! s:find_index(xs, f, ...) abort
let len = len(a:xs)
@@ -426,7 +448,7 @@ function! s:combinations(list, r) abort
if a:r > len(a:list)
return []
elseif a:r < 0
- throw 'vital: Data:List: {r} must be non-negative integer'
+ throw 'vital: Data.List: {r} must be non-negative integer'
endif
let n = len(a:list)
let result = []