Vim script utility library πŸ’“ The sky is the limit!
Switch branches/tags
Nothing to show
Clone or download
Latest commit 5bb164d Apr 1, 2015

README.md

underscore.vim

Build Status Build status

Vim script utility library πŸ’“ The sky is the limit!

underscore.vim is a Vim script library that provides a whole mess of useful functional programming helpers.

echo s:_.reject([1, 2, 3, 4, 5, 6], 'v:val % 2 == 0')
" => [1, 3, 5]

function! s:toFizzBuzz(x)
  return a:x%15 ? a:x%3 ? a:x%5 ? a:x : 'Buzz' : 'Fizz' : 'FizzBuzz'
endfunction

echo s:_._(range(20))
  \.chain()
  \.map('v:val + 1')
  \.filter('v:val % 2')
  \.map(function('s:toFizzBuzz'))
  \.value()
" => [1, 'Fizz', 'Buzz', 7, 'Fizz', 11, 13, 'FizzBuzz', 17, 19]

πŸ“¦ Installation πŸ“¦

  1. Install vital.vim and underscore.vim with your favorite plugin manager.
NeoBundle 'vim-jp/vital.vim'
NeoBundle 'haya14busa/underscore.vim'

Plugin 'vim-jp/vital.vim'
Plugin 'haya14busa/underscore.vim'

Plug 'vim-jp/vital.vim'
Plug 'haya14busa/underscore.vim'
  1. Embed underscore.vim into your plugin with :Vitalize.
:Vitalize . --name={plugin_name} Underscore
  1. Import underscore.vim in your plugins
let s:V = vital#of('vital')
let s:_ = s:V.import('Underscore').import()
echo s:_.filter([1, '2', '3', 4.0, 5], s:_.is_string)
" => ['2', '3']

πŸ“™ Documentation πŸ“™

:h underscore.txt

πŸŽ‰ Examples πŸŽ‰

###🌐 Real world(?) usage 🌐

let s:V = vital#of('vital')
let s:_ = s:V.import('Underscore').import()

function! s:scouter(...)
  let lines = readfile(expand(get(a:, 1, $MYVIMRC)))
  let p = '^\s*$\|^\s*["\\]'
  return s:_.chain(lines).reject(printf("v:val =~# '%s'", p)).length().value()
endfunction

" Count LOC of your vimrc
echo s:scouter()

" Return list of plugins in the given file or your vimrc
function! s:plugins(...)
  let lines = readfile(expand(get(a:, 1, $MYVIMRC)))
  let ignore = '^\s*$\|^\s*["\\]'
  let bundle = "\\v%(%(Neo)?Bundle%(Lazy)|Plug%(in)?) ''([^'']+%(/[^'']+)?)"
  return s:_.chain(lines)
    \.reject(printf("v:val =~# '%s'", ignore))
    \.select(printf("v:val =~# '%s'", bundle))
    \.map(printf("matchlist(v:val, '%s')[1]", bundle))
    \.sort()
    \.value()
endfunction

" Get installed plugins in vimrc
echo s:plugins()
echo len(s:plugins())

πŸ“ Memoize fibonacch πŸ“

let s:V = vital#of('vital')
let s:_ = s:V.import('Underscore').import()

function! s:_fib(n) abort
  return a:n < 2 ? a:n : s:fib(a:n-1) + s:fib(a:n-2)
endfunction

let s:fib = s:_.memoize(function('s:_fib'))
echo s:fib(24)
" => 46368

βœ‰οΈ With Vital.Data.Closure βœ‰οΈ

:h Vital.Data.Closure

let s:V = vital#of('vital')
let s:_ = s:V.import('Underscore').import()
let s:Closure = s:V.import('Data.Closure')
function! s:lambda(x, ...) abort
  return call(s:Closure.build, [a:x] + a:000).to_function()
endfunction

echo s:_.chain(range(1,100))
    \.select(s:lambda('= a:1 % 3 == 0'))
    \.tap(s:lambda(":echo 'len: ' . len(a:1)"))
    \.map(s:lambda('= a:1 * a:1'))
    \.reject(s:lambda('= a:1 < 500'))
    \.drop(3)
    \.foldl(0, s:lambda('+'))
    \.value()
" =>
"   len: 33
"   109296

⚑️ Extend to work with other library ⚑️

You can extend underscore.vim with _.mixin() ✨ See :h _.mixin()

underscore-string.vim

let s:V = vital#of('vital')
let s:_ = s:V.import('Underscore').import()
call s:_.mixin(s:V.import('Data.String'))
call s:_.mixin(s:V.import('Data.String.Interpolation'))
echo s:_.chain("  underscore-${module}.js!  \r\n")
    \.format({'module': 'string'})
    \.replace('.js', '.vim')
    \.chomp()
    \.trim()
    \.pad_left(25, '.')
    \.value()

underscore-random.vim with closure

let s:V = vital#of('vital')
let s:_ = s:V.import('Underscore').import()
call s:_.mixin(s:V.import('Random'))

let s:Closure = s:V.import('Data.Closure')
function! s:lambda(x, ...) abort
    return call(s:Closure.build, [a:x] + a:000).to_function()
endfunction

echo s:_.chain(range(100))
    \.shuffle()
    \.take(3)
    \.foldl(0, s:lambda('+'))
    \.value()
" => random: 155

If you want to see more examples, please see /example directory.

πŸ”— Links πŸ”—

The api and documentation are heavily inspired by Underscore.js, lodash and Underscore.lua. It's implementation are heavily inspired by and taken from vital.vim.

🐦 Author 🐦

haya14busa (https://github.com/haya14busa)