Skip to content

Commit 80f80cf

Browse files
committed
Updated min function to accept key and default kwargs
Added additional autotests for max and min
1 parent 2c70c6a commit 80f80cf

File tree

2 files changed

+46
-22
lines changed

2 files changed

+46
-22
lines changed

transcrypt/development/automated_tests/transcrypt/module_builtin/__init__.py

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,36 @@ def canonizeStringList (stringList):
1212
return [canonizeString (aString) for aString in stringList]
1313

1414
def run (autoTester):
15-
autoTester.check ('min', min (-1.1, -1, -3))
16-
autoTester.check ('max', max (-1.1, -1, -3))
17-
autoTester.check ('max', max ([-1.1, -1, -3]))
18-
autoTester.check ('max', max ((-1.1, -1, -3)))
19-
autoTester.check ('max', max ('abc', 'ABC', 'xyz', 'XYZ'))
20-
autoTester.check ('max', max ('abc', 'ABC', 'xyz', 'XYZ', key=lambda v: v[1]))
21-
autoTester.check ('max', max (['abc', 'ABC', 'xyz', 'XYZ']))
22-
autoTester.check ('max', autoTester.expectException(lambda: max ([])))
23-
autoTester.check ('max', max ([], default='zzz'))
15+
autoTester.check ('min',
16+
min (-1.1, -1, -3),
17+
min ([-1.1, -1, -3]),
18+
min ((-1.1, -1, -3)),
19+
min ('abc', 'ABC', 'xyz', 'XYZ'),
20+
min ('abc', 'ABC', 'xyz', 'XYZ', key=lambda v: v[1]),
21+
min (['abc', 'ABC', 'xyz', 'XYZ']),
22+
min([5,6,7,8,9],[1,2,3,4],key=len),
23+
min([[5,6,7,8,9],[1,2,3,4]],default=[1,1,1],key=len),
24+
min ([], default='zzz'),
25+
)
26+
27+
autoTester.check ('max',
28+
max (-1.1, -1, -3),
29+
max ([-1.1, -1, -3]),
30+
max ((-1.1, -1, -3)),
31+
max ('abc', 'ABC', 'xyz', 'XYZ'),
32+
max ('abc', 'ABC', 'xyz', 'XYZ', key=lambda v: v[1]),
33+
max (['abc', 'ABC', 'xyz', 'XYZ']),
34+
max([5,6,7,8,9],[1,2,3,4],key=len),
35+
max([[5,6,7,8,9],[1,2,3,4]],default=[1,1,1],key=len),
36+
max ([], default='zzz'),
37+
)
38+
# autoTester.check ('max', autoTester.expectException(lambda: max () ))
39+
# autoTester.check ('max', autoTester.expectException(lambda: max (1,2,3,4, default=5) ))
40+
# autoTester.check ('max', autoTester.expectException(lambda: max (default=5)))
41+
# autoTester.check ('max', autoTester.expectException(lambda: max ([])))
42+
# autoTester.check ('max', autoTester.expectException(lambda: max([5,6,7,8,9],[1,2,3,4],default=[1,1,1],key=len) ))
43+
# autoTester.check ('max', autoTester.expectException(lambda: max ([4, 5, 'xyz', 'XYZ']) ))
44+
2445
autoTester.check ('abs', abs (-1), abs (1), abs (0), abs (-0.1), abs (0.1))
2546

2647
autoTester.check ('ord', ord ('a'), ord ('e´'[0])) # This is the 2 codepoint version

transcrypt/modules/org/transcrypt/__builtin__.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -667,8 +667,7 @@ export function ord (aChar) {
667667
return aChar.charCodeAt (0);
668668
};
669669

670-
// Maximum of n values
671-
export function max (...args) {
670+
function min_max (f_compare, ...args) {
672671
// Assume no kwargs
673672
let dflt = undefined;
674673
function key(x) {return x}
@@ -677,33 +676,37 @@ export function max (...args) {
677676
if (args[args.length-1] && args[args.length-1].hasOwnProperty ("__kwargtrans__")) {
678677
const kwargs = args[args.length - 1];
679678
args = args.slice(0, -1);
680-
if (kwargs.hasOwnProperty('key')) key = kwargs['key'];
681679
if (kwargs.hasOwnProperty('py_default')) dflt = kwargs['py_default'];
680+
if (kwargs.hasOwnProperty('key')) key = kwargs['key'];
681+
if (Object.prototype.toString.call(key) !== '[object Function]') throw TypeError("object is not callable", new Error());
682682
}
683683
}
684684

685685
if (args.length === 0) throw TypeError("expected at least 1 argument, got 0", new Error ());
686686
if (args.length > 1 && dflt !== undefined) throw TypeError("Cannot specify a default with multiple positional arguments", new Error ());
687687
if (args.length === 1){
688-
if (Object.prototype.toString.call(args[0]) === '[object Array]'){
689-
args = args[0];
690-
} else {
691-
throw TypeError("object is not iterable", new Error());
692-
}
688+
if (Object.prototype.toString.call(args[0]) !== '[object Array]') throw TypeError("object is not iterable", new Error());
689+
args = args[0];
693690
}
694691
if (args === null || args.length === 0){
695-
if (dflt === undefined) throw ValueError ("max() arg is an empty sequence", new Error ());
692+
if (dflt === undefined) throw ValueError ("arg is an empty sequence", new Error ());
696693
return dflt
697694
}
698695

699-
return args.reduce((max_val, cur_val) => key(cur_val) > key(max_val) ? cur_val : max_val);
696+
return args.reduce((max_val, cur_val) => f_compare(key(cur_val), key(max_val)) ? cur_val : max_val);
697+
}
698+
699+
// Maximum of n values
700+
export function max (...args) {
701+
return min_max(function (a, b){return a > b}, ...args)
700702
// return arguments.length == 1 ? Math.max (...nrOrSeq) : Math.max (...arguments);
701703
}
702704

703705
// Minimum of n numbers
704-
export function min (nrOrSeq) {
705-
return arguments.length == 1 ? Math.min (...nrOrSeq) : Math.min (...arguments);
706-
};
706+
export function min (...args) {
707+
return min_max(function (a, b){return a < b}, ...args)
708+
// return arguments.length == 1 ? Math.min (...nrOrSeq) : Math.min (...arguments);
709+
}
707710

708711
// Integer to binary
709712
export function bin (nbr) {

0 commit comments

Comments
 (0)