From f3fcdf5711e6f7c7a59dd89e7f70af0b3a1c1c44 Mon Sep 17 00:00:00 2001 From: wsdjeg Date: Mon, 13 Mar 2017 20:23:29 +0800 Subject: [PATCH] Add dict apis Thanks for https://github.com/vim-jp/vital.vim --- autoload/SpaceVim/api/data/dict.vim | 99 +++++++++++++++++++++++++++++ docs/apis.md | 2 +- 2 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 autoload/SpaceVim/api/data/dict.vim diff --git a/autoload/SpaceVim/api/data/dict.vim b/autoload/SpaceVim/api/data/dict.vim new file mode 100644 index 000000000..37106ca38 --- /dev/null +++ b/autoload/SpaceVim/api/data/dict.vim @@ -0,0 +1,99 @@ +function! SpaceVim#api#data#dict#get() abort + return map({'make' : '', + \ }, + \ "function('s:' . v:key)" + \ ) +endfunction + +function! s:make(keys, values, ...) abort + let dict = {} + let fill = a:0 ? a:1 : 0 + for i in range(len(a:keys)) + let key = type(a:keys[i]) == type('') ? a:keys[i] : string(a:keys[i]) + if key ==# '' + throw "SpaceVim API: data#dict: Can't use an empty string for key." + endif + let dict[key] = get(a:values, i, fill) + endfor + return dict +endfunction + +" Swaps keys and values +function! s:swap(dict) abort + return s:make(values(a:dict), keys(a:dict)) +endfunction + +" Makes a index dict from a list +function! s:make_index(list, ...) abort + let value = a:0 ? a:1 : 1 + return s:make(a:list, [], value) +endfunction + +function! s:pick(dict, keys) abort + let new_dict = {} + for key in a:keys + if has_key(a:dict, key) + let new_dict[key] = a:dict[key] + endif + endfor + return new_dict +endfunction + +function! s:omit(dict, keys) abort + let new_dict = copy(a:dict) + for key in a:keys + if has_key(a:dict, key) + call remove(new_dict, key) + endif + endfor + return new_dict +endfunction + +function! s:clear(dict) abort + for key in keys(a:dict) + call remove(a:dict, key) + endfor + return a:dict +endfunction + +function! s:_max_by(dict, expr) abort + let dict = s:swap(map(copy(a:dict), a:expr)) + let key = dict[max(keys(dict))] + return [key, a:dict[key]] +endfunction + +function! s:max_by(dict, expr) abort + if empty(a:dict) + throw 'SpaceVim API: data#dict: Empty dictionary' + endif + return s:_max_by(a:dict, a:expr) +endfunction + +function! s:min_by(dict, expr) abort + if empty(a:dict) + throw 'SpaceVim API: data#dict: Empty dictionary' + endif + return s:_max_by(a:dict, '-(' . a:expr . ')') +endfunction + +function! s:_foldl(f, init, xs) abort + let memo = a:init + for [k, v] in a:xs + let expr = substitute(a:f, 'v:key', string(k), 'g') + let expr = substitute(expr, 'v:val', string(v), 'g') + let expr = substitute(expr, 'v:memo', string(memo), 'g') + unlet memo + let memo = eval(expr) + endfor + return memo +endfunction + +function! s:foldl(f, init, dict) abort + return s:_foldl(a:f, a:init, items(a:dict)) +endfunction + +function! s:foldr(f, init, dict) abort + return s:_foldl(a:f, a:init, reverse(items(a:dict))) +endfunction + +" vim:set et sw=2 cc=80: diff --git a/docs/apis.md b/docs/apis.md index 7cdb1441f..fdbd6cb82 100644 --- a/docs/apis.md +++ b/docs/apis.md @@ -4,7 +4,7 @@ title: "APIs" # SpaceVim public APIs: -SpaceVim provide many public apis, you can use this apis in your plugins. +SpaceVim provide many public apis, you can use this apis in your plugins. SpaceVim api got inspired by [vital.vim](https://github.com/vim-jp/vital.vim) ## Usage