Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- " apply.vim
- " Barry Arthur, 12 Sep 2011
- "
- " Inspired by the lispy apply() function, I wanted to see if we could call all
- " of Vim's List functions on strings. It seems that all but two can be, not
- " that all are necessary - sometimes (usually) it's just easier to call the
- " bare function on the bare string.
- "
- " This wasn't created with any serious purpose in mind - more a thought
- " exercise and something interesting to play with.
- "
- " If you find any useful purpose to this, do let me know. :)
- function! Apply(func, string, ...)
- return Apply2(a:func, a:string, ' ', ' ', a:000)
- endfunction
- function! Apply2(func, string, ssep, jsep, ...)
- let rest = []
- if a:0 > 0
- if (type(a:000[0]) == type([])) && (len(a:000) == 1)
- let rest = a:000[0]
- else
- let rest = a:000
- endif
- endif
- let args = extend([split(a:string, a:ssep)], rest)
- let result = call(a:func, args)
- if type(result) == type([])
- return join(result, a:jsep)
- else
- return result
- endif
- endfunction
- " Tests - through all of the List functions mentioned in the same order as
- " Vim's :help list-functions
- let test1 = "this is a test"
- let test2 = "that was my toe""
- echo "get:"
- echo Apply('get', test1, 1, 'default')
- echo Apply('get', test1, 4, 'default')
- echo "len:"
- echo Apply('len', test1)
- echo len(test1)
- echo Apply2('len', test1, '\zs', '')
- echo "empty:"
- echo Apply('empty', test1)
- echo Apply('empty', "")
- echo "insert:"
- echo Apply('insert', test1, "more")
- echo Apply('insert', test1, "more", -1)
- echo Apply('insert', test1, "more", Apply('len', test1))
- echo Apply('add', test1, "added")
- " have to wrap test2 in a list... :-/
- echo "extend:"
- echo Apply('extend', test1, [test2])
- echo "remove:"
- echo Apply('remove', test1, 1)
- echo Apply('remove', test1, 1, 2)
- echo "copy:"
- let test3 = Apply('copy', test1)
- echo test3
- " not really a valid concept for a string... but here for completeness
- echo "deepcopy:"
- let test4 = Apply('deepcopy', test2)
- echo test4
- echo "filter:"
- echo Apply('filter', test1, 'v:val !~ "s"')
- echo Apply('filter', test1, 'v:val !~ "a"')
- echo "map:"
- echo Apply('map', test1, '"<ITEM>".v:val."</ITEM>"')
- echo Apply('map', test1, '"<".v:val.">item</".v:val.">"')
- echo Apply('map', test1, 'toupper(v:val)')
- echo "sort:"
- echo Apply('sort', test1)
- function! RevCompare(i1, i2)
- return a:i1 == a:i2 ? 0 : a:i1 < a:i2 ? 1 : -1
- endfunction
- echo Apply('sort', test1, 'RevCompare')
- function! LenCompare(i1, i2)
- return len(a:i1) == len(a:i2) ? 0 : len(a:i1) > len(a:i2) ? 1 : -1
- endfunction
- echo Apply('sort', test1, 'LenCompare')
- echo "reverse:"
- echo Apply('reverse', test1)
- echo Apply2('reverse', test1, '\zs', '')
- echo Apply2('reverse', Apply('reverse', test1), '\zs', '')
- " split broken for obviously guessable reasons - unnecessary?
- echo "split: broken"
- " echo Apply('split', test1, ' ')
- echo "join:"
- echo Apply('join', test1, '-')
- " range broken - expects numbers (not the lists the strings are split into)
- " echo Apply('range', "10")
- " unexpected - this "looks" like split() but it is just a string, of course
- echo "string:"
- echo Apply('string', test1)
- echo Apply2('string', test1, '\zs', ' ')
- echo Apply2('string', "one:two:three", ':', ' ')
- echo type("") == type("")
- echo type([]) == type([])
- echo type(Apply2('string', "one:two:three", ':', ' ')) == type("")
- echo type(Apply2('string', "one:two:three", ':', ' ')) == type([])
- echo "index:"
- echo Apply('index', test1, 'is')
- echo Apply('index', test1, 'a')
- echo Apply('index', test1, 'frog')
- echo "max:"
- let test5 = "10 23 45 67 1 28 45 23 19"
- let test6 = "10:23:45:67:1:28:45:23:19"
- echo Apply('max', test5)
- echo Apply2('max', test6, ':', ' ')
- echo "min:"
- echo Apply('min', test5)
- echo Apply2('min', test6, ':', ' ')
- echo "count:"
- echo Apply('count', test1, 'is')
- let test7 = "and an apple and a cat"
- echo Apply('count', test7, 'a')
- echo Apply('count', test5, '23')
- echo "repeat:"
- echo Apply('repeat', test1, 2)
Advertisement
Add Comment
Please, Sign In to add comment