bairui

apply.vim

Sep 12th, 2011
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VIM 3.96 KB | None | 0 0
  1. " apply.vim
  2. " Barry Arthur, 12 Sep 2011
  3. "
  4. " Inspired by the lispy apply() function, I wanted to see if we could call all
  5. " of Vim's List functions on strings. It seems that all but two can be, not
  6. " that all are necessary - sometimes (usually) it's just easier to call the
  7. " bare function on the bare string.
  8. "
  9. " This wasn't created with any serious purpose in mind - more a thought
  10. " exercise and something interesting to play with.
  11. "
  12. " If you find any useful purpose to this, do let me know. :)
  13.  
  14. function! Apply(func, string, ...)
  15.   return Apply2(a:func, a:string, ' ', ' ', a:000)
  16. endfunction
  17.  
  18. function! Apply2(func, string, ssep, jsep, ...)
  19.   let rest = []
  20.   if a:0 > 0
  21.     if (type(a:000[0]) == type([])) && (len(a:000) == 1)
  22.       let rest = a:000[0]
  23.     else
  24.       let rest = a:000
  25.     endif
  26.   endif
  27.   let args = extend([split(a:string, a:ssep)], rest)
  28.   let result = call(a:func, args)
  29.   if type(result) == type([])
  30.     return join(result, a:jsep)
  31.   else
  32.     return result
  33.   endif
  34. endfunction
  35.  
  36. " Tests - through all of the List functions mentioned in the same order as
  37. " Vim's   :help list-functions
  38.  
  39. let test1 = "this is a test"
  40. let test2 = "that was my toe""
  41.  
  42. echo "get:"
  43. echo Apply('get', test1, 1, 'default')
  44. echo Apply('get', test1, 4, 'default')
  45.  
  46. echo "len:"
  47. echo Apply('len', test1)
  48. echo len(test1)
  49. echo Apply2('len', test1, '\zs', '')
  50.  
  51. echo "empty:"
  52. echo Apply('empty', test1)
  53. echo Apply('empty', "")
  54.  
  55. echo "insert:"
  56. echo Apply('insert', test1, "more")
  57. echo Apply('insert', test1, "more", -1)
  58. echo Apply('insert', test1, "more", Apply('len', test1))
  59.  
  60. echo Apply('add', test1, "added")
  61.  
  62. " have to wrap test2 in a list... :-/
  63. echo "extend:"
  64. echo Apply('extend', test1, [test2])
  65.  
  66. echo "remove:"
  67. echo Apply('remove', test1, 1)
  68. echo Apply('remove', test1, 1, 2)
  69.  
  70. echo "copy:"
  71. let test3 = Apply('copy', test1)
  72. echo test3
  73.  
  74. " not really a valid concept for a string... but here for completeness
  75. echo "deepcopy:"
  76. let test4 = Apply('deepcopy', test2)
  77. echo test4
  78.  
  79. echo "filter:"
  80. echo Apply('filter', test1, 'v:val !~ "s"')
  81. echo Apply('filter', test1, 'v:val !~ "a"')
  82.  
  83. echo "map:"
  84. echo Apply('map', test1, '"<ITEM>".v:val."</ITEM>"')
  85. echo Apply('map', test1, '"<".v:val.">item</".v:val.">"')
  86. echo Apply('map', test1, 'toupper(v:val)')
  87.  
  88. echo "sort:"
  89. echo Apply('sort', test1)
  90. function! RevCompare(i1, i2)
  91.   return a:i1 == a:i2 ? 0 : a:i1 < a:i2 ? 1 : -1
  92. endfunction
  93. echo Apply('sort', test1, 'RevCompare')
  94. function! LenCompare(i1, i2)
  95.   return len(a:i1) == len(a:i2) ? 0 : len(a:i1) > len(a:i2) ? 1 : -1
  96. endfunction
  97. echo Apply('sort', test1, 'LenCompare')
  98.  
  99. echo "reverse:"
  100. echo Apply('reverse', test1)
  101. echo Apply2('reverse', test1, '\zs', '')
  102. echo Apply2('reverse', Apply('reverse', test1), '\zs', '')
  103.  
  104. " split broken for obviously guessable reasons - unnecessary?
  105. echo "split: broken"
  106. " echo Apply('split', test1, ' ')
  107.  
  108. echo "join:"
  109. echo Apply('join', test1, '-')
  110.  
  111. " range broken - expects numbers (not the lists the strings are split into)
  112. " echo Apply('range', "10")
  113.  
  114. " unexpected - this "looks" like split() but it is just a string, of course
  115. echo "string:"
  116. echo Apply('string', test1)
  117. echo Apply2('string', test1, '\zs', ' ')
  118. echo Apply2('string', "one:two:three", ':', ' ')
  119. echo type("") == type("")
  120. echo type([]) == type([])
  121. echo type(Apply2('string', "one:two:three", ':', ' ')) == type("")
  122. echo type(Apply2('string', "one:two:three", ':', ' ')) == type([])
  123.  
  124. echo "index:"
  125. echo Apply('index', test1, 'is')
  126. echo Apply('index', test1, 'a')
  127. echo Apply('index', test1, 'frog')
  128.  
  129. echo "max:"
  130. let test5 = "10 23 45 67 1 28 45 23 19"
  131. let test6 = "10:23:45:67:1:28:45:23:19"
  132. echo Apply('max', test5)
  133. echo Apply2('max', test6, ':', ' ')
  134.  
  135. echo "min:"
  136. echo Apply('min', test5)
  137. echo Apply2('min', test6, ':', ' ')
  138.  
  139. echo "count:"
  140. echo Apply('count', test1, 'is')
  141. let test7 = "and an apple and a cat"
  142. echo Apply('count', test7, 'a')
  143. echo Apply('count', test5, '23')
  144.  
  145. echo "repeat:"
  146. echo Apply('repeat', test1, 2)
Advertisement
Add Comment
Please, Sign In to add comment