@@ -1399,35 +1399,39 @@ def sort(coll, f=compare) -> Optional[ISeq]:
1399
1399
using f or use the `compare` fn if not.
1400
1400
1401
1401
The comparator fn can be either a boolean or 3-way comparison fn."""
1402
- if isinstance (coll , IPersistentMap ):
1403
- coll = lseq .to_seq (coll )
1402
+ seq = lseq .to_seq (coll )
1403
+ if seq :
1404
+ if isinstance (coll , IPersistentMap ):
1405
+ coll = seq
1404
1406
1405
- comparator = _fn_to_comparator (f )
1407
+ comparator = _fn_to_comparator (f )
1406
1408
1407
- class key :
1408
- __slots__ = ("obj" ,)
1409
+ class key :
1410
+ __slots__ = ("obj" ,)
1409
1411
1410
- def __init__ (self , obj ):
1411
- self .obj = obj
1412
+ def __init__ (self , obj ):
1413
+ self .obj = obj
1412
1414
1413
- def __lt__ (self , other ):
1414
- return comparator (self .obj , other .obj ) < 0
1415
+ def __lt__ (self , other ):
1416
+ return comparator (self .obj , other .obj ) < 0
1415
1417
1416
- def __gt__ (self , other ):
1417
- return comparator (self .obj , other .obj ) > 0
1418
+ def __gt__ (self , other ):
1419
+ return comparator (self .obj , other .obj ) > 0
1418
1420
1419
- def __eq__ (self , other ):
1420
- return comparator (self .obj , other .obj ) == 0
1421
+ def __eq__ (self , other ):
1422
+ return comparator (self .obj , other .obj ) == 0
1421
1423
1422
- def __le__ (self , other ):
1423
- return comparator (self .obj , other .obj ) <= 0
1424
+ def __le__ (self , other ):
1425
+ return comparator (self .obj , other .obj ) <= 0
1424
1426
1425
- def __ge__ (self , other ):
1426
- return comparator (self .obj , other .obj ) >= 0
1427
+ def __ge__ (self , other ):
1428
+ return comparator (self .obj , other .obj ) >= 0
1427
1429
1428
- __hash__ = None # type: ignore
1430
+ __hash__ = None # type: ignore
1429
1431
1430
- return lseq .sequence (sorted (coll , key = key ))
1432
+ return lseq .sequence (sorted (coll , key = key ))
1433
+ else :
1434
+ return llist .EMPTY
1431
1435
1432
1436
1433
1437
def sort_by (keyfn , coll , cmp = compare ) -> Optional [ISeq ]:
@@ -1436,35 +1440,39 @@ def sort_by(keyfn, coll, cmp=compare) -> Optional[ISeq]:
1436
1440
using cmp or use the `compare` fn if not.
1437
1441
1438
1442
The comparator fn can be either a boolean or 3-way comparison fn."""
1439
- if isinstance (coll , IPersistentMap ):
1440
- coll = lseq .to_seq (coll )
1443
+ seq = lseq .to_seq (coll )
1444
+ if seq :
1445
+ if isinstance (coll , IPersistentMap ):
1446
+ coll = seq
1441
1447
1442
- comparator = _fn_to_comparator (cmp )
1448
+ comparator = _fn_to_comparator (cmp )
1443
1449
1444
- class key :
1445
- __slots__ = ("obj" ,)
1450
+ class key :
1451
+ __slots__ = ("obj" ,)
1446
1452
1447
- def __init__ (self , obj ):
1448
- self .obj = obj
1453
+ def __init__ (self , obj ):
1454
+ self .obj = obj
1449
1455
1450
- def __lt__ (self , other ):
1451
- return comparator (keyfn (self .obj ), keyfn (other .obj )) < 0
1456
+ def __lt__ (self , other ):
1457
+ return comparator (keyfn (self .obj ), keyfn (other .obj )) < 0
1452
1458
1453
- def __gt__ (self , other ):
1454
- return comparator (keyfn (self .obj ), keyfn (other .obj )) > 0
1459
+ def __gt__ (self , other ):
1460
+ return comparator (keyfn (self .obj ), keyfn (other .obj )) > 0
1455
1461
1456
- def __eq__ (self , other ):
1457
- return comparator (keyfn (self .obj ), keyfn (other .obj )) == 0
1462
+ def __eq__ (self , other ):
1463
+ return comparator (keyfn (self .obj ), keyfn (other .obj )) == 0
1458
1464
1459
- def __le__ (self , other ):
1460
- return comparator (keyfn (self .obj ), keyfn (other .obj )) <= 0
1465
+ def __le__ (self , other ):
1466
+ return comparator (keyfn (self .obj ), keyfn (other .obj )) <= 0
1461
1467
1462
- def __ge__ (self , other ):
1463
- return comparator (keyfn (self .obj ), keyfn (other .obj )) >= 0
1468
+ def __ge__ (self , other ):
1469
+ return comparator (keyfn (self .obj ), keyfn (other .obj )) >= 0
1464
1470
1465
- __hash__ = None # type: ignore
1471
+ __hash__ = None # type: ignore
1466
1472
1467
- return lseq .sequence (sorted (coll , key = key ))
1473
+ return lseq .sequence (sorted (coll , key = key ))
1474
+ else :
1475
+ return llist .EMPTY
1468
1476
1469
1477
1470
1478
def is_special_form (s : sym .Symbol ) -> bool :
0 commit comments