bairui

some Array Utilities for Vim

Sep 30th, 2011
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VIM 3.44 KB | None | 0 0
  1. " Array Utilities
  2. " Barry Arthur 20100802
  3.  
  4. " Zip(array_a, array_b, method)
  5. "
  6. " join each element of array a with the corresponding element of array b
  7. " Use the third argument, method, to dictate how the elements should be
  8. " combined:
  9. " given: a = [a, b]
  10. "        b = [1, 2]
  11. " 0 = flattened array  : [a, 1, b, 2]
  12. " 1 = array groups     : [[a, 1], [b, 2]]
  13. " x = join separator x : [ax1, bx2]
  14. "
  15. " NOTE: If one array is longer than the other, the tail of that array is added
  16. " to the result.
  17. function! Zip(a, b, method)
  18.   let i = 0
  19.   let r = []
  20.   let l_a = len(a:a)
  21.   let l_b = len(a:b)
  22.   let n = min([len(a:a), len(a:b)])
  23.   while i < n
  24.     if a:method == "0"
  25.       call add(r, a:a[i])
  26.       call add(r, a:b[i])
  27.     elseif a:method == "1"
  28.       call add(r, [a:a[i], a:b[i]])
  29.     else
  30.       call add(r, join([a:a[i], a:b[i]], a:method))
  31.     endif
  32.     let i+= 1
  33.   endwhile
  34.   if l_a == l_b
  35.     return r
  36.   elseif l_a > l_b
  37.     exe "return r + a:a[" . n . ":]"
  38.   else
  39.     exe "return r + a:b[" . n . ":]"
  40.   endif
  41. endfunction
  42.  
  43. "let lst1 = ['a', 'b', 'c']
  44. "let lst2= [1, 2, 3]
  45. "let lst3 = Zip(lst1, lst2, 0)
  46. "echo lst3
  47. "let lst3 = Zip(lst1, lst2, 1)
  48. "echo lst3
  49. "let lst3 = Zip(lst1, lst2, ':')
  50. "echo lst3
  51. "let lst3 = Zip(lst1, lst2, '')
  52. "echo lst3
  53. "
  54. "let lst1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
  55. "let lst2= [1, 2, 3]
  56. "let lst3 = Zip(lst1, lst2, 0)
  57. "echo lst3
  58. "let lst3 = Zip(lst1, lst2, 1)
  59. "echo lst3
  60. "let lst3 = Zip(lst1, lst2, ':')
  61. "echo lst3
  62. "
  63. "let lst1 = ['a', 'b', 'c']
  64. "let lst2= [1, 2, 3, 4, 5, 6]
  65. "let lst3 = Zip(lst1, lst2, 0)
  66. "echo lst3
  67. "let lst3 = Zip(lst1, lst2, 1)
  68. "echo lst3
  69. "let lst3 = Zip(lst1, lst2, ':')
  70. "echo lst3
  71.  
  72. "------------------------------
  73. " George Marsaglia's Multiply-with-carry Random Number Generator with some modifications {{{2
  74. let s:m_w = matchstr(tempname(), '\d\+') * getpid()
  75. let s:m_z = localtime()
  76.  
  77. function! RandomNumber()
  78.   let s:m_z = s:m_z + (s:m_z / 4)
  79.   let s:m_w = s:m_w + (s:m_w / 4)
  80.   return abs((s:m_z) + s:m_w)
  81. endfunction
  82. " end RNG }}}2
  83.  
  84. "let a = [1, 3, 5, 2, 8, 6, 7, 9, 4]
  85. "let b = sort(a)
  86.  
  87. function! Shuffle(a)
  88.   let b = deepcopy(a:a)
  89.   let n = 0
  90.   let length = len(b)
  91.   while n < length
  92.     let tmp = b[n]
  93.     let dst = RandomNumber() % length
  94.     let b[n] = b[dst]
  95.     let b[dst] = tmp
  96.     let n += 1
  97.   endwhile
  98.   return b
  99. endfunction
  100.  
  101. "echo b
  102. "echo Shuffle(b)
  103.  
  104. "--------------------------------------------------------------------------------
  105.  
  106. " function! Paste(a, b, join, sep)
  107. " Emulate the unix paste command
  108. " Arguments:
  109. "   a    - list, or string containing 'sep' (default='\n') delimited elements
  110. "   b    - list, or string containing 'sep' (default='\n') delimited elements
  111. "   join - separator (default=' ') to use when joining each respective line of
  112. "          a and b
  113. "   sep  - separator (default='\n') to use when splitting a and b (if strings)
  114. " e.g.
  115. " ------v yank following into register a - "a3yy
  116. " one
  117. " two
  118. " three
  119. " ------v yank following into register b - "b3yy
  120. " satu
  121. " dua
  122. " tiga
  123. " call append('.', Paste(@a, @b))
  124. function! Paste(a, b, ...)
  125.   let join = (a:0 >= 1) ? a:1 : ' '
  126.   let sep = (a:0 == 2) ? a:2 : '\n'
  127.   if type(a:a) == 1
  128.     let a = split(a:a, sep)
  129.     let b = split(a:b, sep)
  130.   else
  131.     let a = a:a
  132.     let b = a:b
  133.   end
  134.   return Zip(a, b, join)
  135. endfunction
  136.  
  137. "one
  138. "two
  139. "three
  140. "
  141. "satu
  142. "dua
  143. "tiga
  144. "
  145. "let xa = ['one', 'two', 'three']
  146. "let xb = ['satu', 'dua', 'tiga']
  147. "['one', 'two', 'three']
  148.  
  149.  
Advertisement
Add Comment
Please, Sign In to add comment