1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

arrays: implement python-inspired array zip function and test (#8667)

This commit is contained in:
nyx-litenite
2021-03-07 04:58:13 -05:00
committed by GitHub
parent 82085b0140
commit a902178fdb
2 changed files with 60 additions and 0 deletions

View File

@ -98,6 +98,33 @@ pub fn merge<T>(a []T, b []T) []T {
return m
}
// group n arrays into a single array of arrays with n elements
pub fn group<T>(lists ...[]T) [][]T {
mut length := if lists.len > 0 { lists[0].len } else { 0 }
// calculate length of output by finding shortest input array
for ndx in 1 .. lists.len {
if lists[ndx].len < length {
length = lists[ndx].len
}
}
if length > 0 {
mut arr := [][]T{cap: length}
// append all combined arrays into the resultant array
for ndx in 0 .. length {
mut zipped := []T{cap: lists.len}
// combine each list item for the ndx position into one array
for list_ndx in 0 .. lists.len {
zipped << lists[list_ndx][ndx]
}
arr << zipped
}
return arr
}
return [][]T{}
}
[deprecated]
pub fn shuffle<T>(mut a []T, n int) {
panic('Please use rand.util.shuffle() instead')