Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- " Array Utilities
- " Barry Arthur 20100802
- " Zip(array_a, array_b, method)
- "
- " join each element of array a with the corresponding element of array b
- " Use the third argument, method, to dictate how the elements should be
- " combined:
- " given: a = [a, b]
- " b = [1, 2]
- " 0 = flattened array : [a, 1, b, 2]
- " 1 = array groups : [[a, 1], [b, 2]]
- " x = join separator x : [ax1, bx2]
- "
- " NOTE: If one array is longer than the other, the tail of that array is added
- " to the result.
- function! Zip(a, b, method)
- let i = 0
- let r = []
- let l_a = len(a:a)
- let l_b = len(a:b)
- let n = min([len(a:a), len(a:b)])
- while i < n
- if a:method == "0"
- call add(r, a:a[i])
- call add(r, a:b[i])
- elseif a:method == "1"
- call add(r, [a:a[i], a:b[i]])
- else
- call add(r, join([a:a[i], a:b[i]], a:method))
- endif
- let i+= 1
- endwhile
- if l_a == l_b
- return r
- elseif l_a > l_b
- exe "return r + a:a[" . n . ":]"
- else
- exe "return r + a:b[" . n . ":]"
- endif
- endfunction
- "let lst1 = ['a', 'b', 'c']
- "let lst2= [1, 2, 3]
- "let lst3 = Zip(lst1, lst2, 0)
- "echo lst3
- "let lst3 = Zip(lst1, lst2, 1)
- "echo lst3
- "let lst3 = Zip(lst1, lst2, ':')
- "echo lst3
- "let lst3 = Zip(lst1, lst2, '')
- "echo lst3
- "
- "let lst1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
- "let lst2= [1, 2, 3]
- "let lst3 = Zip(lst1, lst2, 0)
- "echo lst3
- "let lst3 = Zip(lst1, lst2, 1)
- "echo lst3
- "let lst3 = Zip(lst1, lst2, ':')
- "echo lst3
- "
- "let lst1 = ['a', 'b', 'c']
- "let lst2= [1, 2, 3, 4, 5, 6]
- "let lst3 = Zip(lst1, lst2, 0)
- "echo lst3
- "let lst3 = Zip(lst1, lst2, 1)
- "echo lst3
- "let lst3 = Zip(lst1, lst2, ':')
- "echo lst3
- "------------------------------
- " George Marsaglia's Multiply-with-carry Random Number Generator with some modifications {{{2
- let s:m_w = matchstr(tempname(), '\d\+') * getpid()
- let s:m_z = localtime()
- function! RandomNumber()
- let s:m_z = s:m_z + (s:m_z / 4)
- let s:m_w = s:m_w + (s:m_w / 4)
- return abs((s:m_z) + s:m_w)
- endfunction
- " end RNG }}}2
- "let a = [1, 3, 5, 2, 8, 6, 7, 9, 4]
- "let b = sort(a)
- function! Shuffle(a)
- let b = deepcopy(a:a)
- let n = 0
- let length = len(b)
- while n < length
- let tmp = b[n]
- let dst = RandomNumber() % length
- let b[n] = b[dst]
- let b[dst] = tmp
- let n += 1
- endwhile
- return b
- endfunction
- "echo b
- "echo Shuffle(b)
- "--------------------------------------------------------------------------------
- " function! Paste(a, b, join, sep)
- " Emulate the unix paste command
- " Arguments:
- " a - list, or string containing 'sep' (default='\n') delimited elements
- " b - list, or string containing 'sep' (default='\n') delimited elements
- " join - separator (default=' ') to use when joining each respective line of
- " a and b
- " sep - separator (default='\n') to use when splitting a and b (if strings)
- " e.g.
- " ------v yank following into register a - "a3yy
- " one
- " two
- " three
- " ------v yank following into register b - "b3yy
- " satu
- " dua
- " tiga
- " call append('.', Paste(@a, @b))
- function! Paste(a, b, ...)
- let join = (a:0 >= 1) ? a:1 : ' '
- let sep = (a:0 == 2) ? a:2 : '\n'
- if type(a:a) == 1
- let a = split(a:a, sep)
- let b = split(a:b, sep)
- else
- let a = a:a
- let b = a:b
- end
- return Zip(a, b, join)
- endfunction
- "one
- "two
- "three
- "
- "satu
- "dua
- "tiga
- "
- "let xa = ['one', 'two', 'three']
- "let xb = ['satu', 'dua', 'tiga']
- "['one', 'two', 'three']
Advertisement
Add Comment
Please, Sign In to add comment