|
865 | 865 | "Sum the arguments together. If no arguments given, returns 0."
|
866 | 866 | ([] 0)
|
867 | 867 | ([x] x)
|
868 |
| - ([x & args] |
869 |
| - (if (seq (rest args)) |
870 |
| - (recur (operator/add x (first args)) (rest args)) |
871 |
| - (operator/add x (first args))))) |
| 868 | + ([x y] (operator/add x y)) |
| 869 | + ([x y & args] |
| 870 | + (if (seq args) |
| 871 | + (recur (operator/add x y) (first args) (rest args)) |
| 872 | + (operator/add x y)))) |
872 | 873 |
|
873 | 874 | (defn -
|
874 | 875 | "Subtract the arguments. If one argument given, returns the negation
|
875 | 876 | of that argument."
|
876 | 877 | ([x] (operator/neg x))
|
877 |
| - ([x & args] |
878 |
| - (if (seq (rest args)) |
879 |
| - (recur (operator/sub x (first args)) (rest args)) |
880 |
| - (operator/sub x (first args))))) |
| 878 | + ([x y] (operator/sub x y)) |
| 879 | + ([x y & args] |
| 880 | + (if (seq args) |
| 881 | + (recur (operator/sub x y) (first args) (rest args)) |
| 882 | + (operator/sub x y)))) |
881 | 883 |
|
882 | 884 | (defn *
|
883 | 885 | "Multiply the arguments. If no arguments given, returns 1."
|
884 | 886 | ([] 1)
|
885 | 887 | ([x] x)
|
886 |
| - ([x & args] |
887 |
| - (if (seq (rest args)) |
888 |
| - (recur (operator/mul x (first args)) (rest args)) |
889 |
| - (operator/mul x (first args))))) |
| 888 | + ([x y] (operator/mul x y)) |
| 889 | + ([x y & args] |
| 890 | + (if (seq args) |
| 891 | + (recur (operator/mul x y) (first args) (rest args)) |
| 892 | + (operator/mul x y)))) |
890 | 893 |
|
891 | 894 | (defn /
|
892 | 895 | "Divide the arguments. If no arguments given, returns the inverse of
|
893 | 896 | the argument."
|
894 | 897 | ([x] (basilisp.lang.runtime/divide 1 x))
|
895 |
| - ([x & args] |
896 |
| - (if (seq (rest args)) |
897 |
| - (recur (basilisp.lang.runtime/divide x (first args)) (rest args)) |
898 |
| - (basilisp.lang.runtime/divide x (first args))))) |
| 898 | + ([x y] (basilisp.lang.runtime/divide x y)) |
| 899 | + ([x y & args] |
| 900 | + (if (seq args) |
| 901 | + (recur (basilisp.lang.runtime/divide x y) (first args) (rest args)) |
| 902 | + (basilisp.lang.runtime/divide x y)))) |
899 | 903 |
|
900 | 904 | (defn mod
|
901 | 905 | "Returns the modulo of num and div."
|
|
1325 | 1329 | [x]
|
1326 | 1330 | (python/int x))
|
1327 | 1331 |
|
| 1332 | +;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 1333 | +;; Unchecked Arithmetic ;; |
| 1334 | +;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 1335 | + |
| 1336 | +(defmacro ^:private defcopy |
| 1337 | + "Copy the Var `var` into a new Var as `name`. |
| 1338 | + |
| 1339 | + Append the given docstring `doc` to the end of the new Var's `doc`." |
| 1340 | + ([name var] |
| 1341 | + `(defcopy ~name nil ~var)) |
| 1342 | + ([name doc var] |
| 1343 | + (let [orig-var (basilisp.lang.runtime/resolve-var var *ns*) |
| 1344 | + orig-var-meta (meta orig-var) |
| 1345 | + var-doc (:doc orig-var-meta) |
| 1346 | + new-doc (if doc |
| 1347 | + (str var-doc "\n\n" doc) |
| 1348 | + var-doc) |
| 1349 | + vname (vary-meta name |
| 1350 | + assoc |
| 1351 | + :doc new-doc |
| 1352 | + :arglists (list 'quote (:arglists orig-var-meta)))] |
| 1353 | + `(def ~vname ~var)))) |
| 1354 | + |
| 1355 | +(defcopy unchecked-add |
| 1356 | + "Same as '+. Python integers are unlimited precision, so unchecked arithmetic |
| 1357 | + is only provided for compatibility with platforms without unlimited precision |
| 1358 | + integers." |
| 1359 | + +) |
| 1360 | +(defcopy unchecked-add-int |
| 1361 | + "Same as '+. Python integers are unlimited precision, so unchecked arithmetic |
| 1362 | + is only provided for compatibility with platforms without unlimited precision |
| 1363 | + integers." |
| 1364 | + +) |
| 1365 | +(defcopy unchecked-subtract |
| 1366 | + "Same as '-. Python integers are unlimited precision, so unchecked arithmetic |
| 1367 | + is only provided for compatibility with platforms without unlimited precision |
| 1368 | + integers." |
| 1369 | + -) |
| 1370 | +(defcopy unchecked-subtract-int |
| 1371 | + "Same as '-. Python integers are unlimited precision, so unchecked arithmetic |
| 1372 | + is only provided for compatibility with platforms without unlimited precision |
| 1373 | + integers." |
| 1374 | + -) |
| 1375 | +(defcopy unchecked-multiply |
| 1376 | + "Same as '*. Python integers are unlimited precision, so unchecked arithmetic |
| 1377 | + is only provided for compatibility with platforms without unlimited precision |
| 1378 | + integers." |
| 1379 | + *) |
| 1380 | +(defcopy unchecked-multiply-int |
| 1381 | + "Same as '*. Python integers are unlimited precision, so unchecked arithmetic |
| 1382 | + is only provided for compatibility with platforms without unlimited precision |
| 1383 | + integers." |
| 1384 | + *) |
| 1385 | +(defcopy unchecked-divide-int |
| 1386 | + "Same as '/. Python integers are unlimited precision, so unchecked arithmetic |
| 1387 | + is only provided for compatibility with platforms without unlimited precision |
| 1388 | + integers." |
| 1389 | + /) |
| 1390 | + |
| 1391 | +(defcopy unchecked-inc |
| 1392 | + "Same as 'inc. Python integers are unlimited precision, so unchecked arithmetic |
| 1393 | + is only provided for compatibility with platforms without unlimited precision |
| 1394 | + integers." |
| 1395 | + inc) |
| 1396 | +(defcopy unchecked-inc-int |
| 1397 | + "Same as 'inc. Python integers are unlimited precision, so unchecked arithmetic |
| 1398 | + is only provided for compatibility with platforms without unlimited precision |
| 1399 | + integers." |
| 1400 | + inc) |
| 1401 | +(defcopy unchecked-dec |
| 1402 | + "Same as 'dec. Python integers are unlimited precision, so unchecked arithmetic |
| 1403 | + is only provided for compatibility with platforms without unlimited precision |
| 1404 | + integers." |
| 1405 | + dec) |
| 1406 | +(defcopy unchecked-dec-int |
| 1407 | + "Same as 'dec. Python integers are unlimited precision, so unchecked arithmetic |
| 1408 | + is only provided for compatibility with platforms without unlimited precision |
| 1409 | + integers." |
| 1410 | + dec) |
| 1411 | + |
| 1412 | +(defn unchecked-negate |
| 1413 | + "Return the negation of x. |
| 1414 | + |
| 1415 | + Same as (- x). Python integers are unlimited precision so unchecked arithmetic |
| 1416 | + is only provided for compatibility with platforms without unlimited precision |
| 1417 | + integers." |
| 1418 | + [x] |
| 1419 | + (- x)) |
| 1420 | +(defcopy unchecked-negate-int unchecked-negate) |
| 1421 | + |
| 1422 | +(defn unchecked-byte |
| 1423 | + "Coerce x to a byte. Value may be truncated or rounded." |
| 1424 | + [x] |
| 1425 | + (byte (mod x 256))) |
| 1426 | + |
| 1427 | +(defn unchecked-char |
| 1428 | + "Coerce x to a char. Value may be truncated or rounded." |
| 1429 | + [x] |
| 1430 | + (cond |
| 1431 | + (instance? python/int x) (char (mod x sys/maxunicode)) |
| 1432 | + :else (char x))) |
| 1433 | + |
| 1434 | +(defcopy unchecked-double |
| 1435 | + "Same as 'double. Python integers are unlimited precision, so unchecked arithmetic |
| 1436 | + is only provided for compatibility with platforms without unlimited precision |
| 1437 | + integers." |
| 1438 | + double) |
| 1439 | +(defcopy unchecked-float |
| 1440 | + "Same as 'float. Python integers are unlimited precision, so unchecked arithmetic |
| 1441 | + is only provided for compatibility with platforms without unlimited precision |
| 1442 | + integers." |
| 1443 | + float) |
| 1444 | + |
| 1445 | +(defcopy unchecked-int |
| 1446 | + "Same as 'int. Python integers are unlimited precision, so unchecked arithmetic |
| 1447 | + is only provided for compatibility with platforms without unlimited precision |
| 1448 | + integers." |
| 1449 | + int) |
| 1450 | +(defcopy unchecked-long |
| 1451 | + "Same as 'long. Python integers are unlimited precision, so unchecked arithmetic |
| 1452 | + is only provided for compatibility with platforms without unlimited precision |
| 1453 | + integers." |
| 1454 | + long) |
| 1455 | +(defcopy unchecked-short |
| 1456 | + "Same as 'short. Python integers are unlimited precision, so unchecked arithmetic |
| 1457 | + is only provided for compatibility with platforms without unlimited precision |
| 1458 | + integers." |
| 1459 | + short) |
| 1460 | + |
1328 | 1461 | ;;;;;;;;;;;;;;;;
|
1329 | 1462 | ;; Exceptions ;;
|
1330 | 1463 | ;;;;;;;;;;;;;;;;
|
|
0 commit comments