(zip [l1] [l2])

This commit is contained in:
2020-12-05 19:06:15 -07:00
parent 609f14656a
commit 809a39ffe1
4 changed files with 47 additions and 7 deletions

View File

@@ -158,6 +158,34 @@ class Prelude {
return fullGroups;
}
public static function zip<A, B>(a:Array<A>, b:Array<B>, extraHandling = Drop):Array<Array<Dynamic>> {
var max = Math.floor(if (a.length != b.length) {
switch (extraHandling) {
case Throw:
throw 'zip was given lists of mis-matched size: $a, $b';
case Keep:
Math.max(a.length, b.length);
case Drop:
Math.min(a.length, b.length);
}
} else {
a.length;
});
return [
for (idx in 0...max) [
if (idx < a.length)
a[idx]
else
null,
if (idx < b.length)
b[idx]
else
null
]
];
}
public static dynamic function truthy(v:Any) {
return switch (Type.typeof(v)) {
case TNull: false;