Created
March 11, 2019 11:13
-
-
Save ujihisa/18eb2e0f2cf5b1a402fccf97ea6e494c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let s:S = vital#vital#import('Data.String') | |
let s:L = vital#vital#import('Data.List') | |
function! s:count_words(text) abort | |
let F = { memo, x -> has_key(memo, x) | |
\ ? extend(memo, {x: memo[x] + 1}) | |
\ : extend(memo, {x: 1})} | |
return s:L.foldl(F, {}, s:S.scan(a:text, '\w\+')) | |
endfunction | |
echo s:count_words('this is a pen or not a pen') | |
" {'this': 1, 'is': 1, 'a': 2, 'pen': 2, ...} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment