Skip to content

Instantly share code, notes, and snippets.

@avenwu
Created July 1, 2020 10:35
Show Gist options
  • Save avenwu/06788899179d05abdc55c633992d20de to your computer and use it in GitHub Desktop.
Save avenwu/06788899179d05abdc55c633992d20de to your computer and use it in GitHub Desktop.
Combine two list into one with cross order, example: [A,B,C], [D,E,F] => [A,D,B,E,C,F]
import 'dart:math';
extension ListExtension<R> on List<R> {
// combine two list
// [A,B,C], [D,E,F] => [A,D,B,E,C,F]
List<R> merge(List<R> list) {
List<R> output = [];
var minLength = min(length, list.length);
var maxLength = max(length, list.length);
for (var i = 0; i < minLength; i++) {
output.add(this[i]);
output.add(list[i]);
}
List<R> longer = length > list.length ? this : list;
for (var i = minLength; i < maxLength; i++) {
output.add(longer[i]);
}
return output;
}
// join the list
// [A,B,C], D => [A,D,B,C,D]
List<R> joinList(R item) {
return merge(List.generate(length, (index) => item));
}
Iterable<MapEntry<int, R>> entry() sync* {
int index = 0;
for (R item in this) {
yield MapEntry(index, item);
index = index + 1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment