|
983 | 983 | [x]
|
984 | 984 | (instance? python/bytes x))
|
985 | 985 |
|
| 986 | +(defn char? |
| 987 | + "Return true if x is a string of length 1." |
| 988 | + [x] |
| 989 | + (and (instance? python/str x) |
| 990 | + (= (python/len x) 1))) |
| 991 | + |
986 | 992 | (defn class?
|
987 | 993 | "Return true if x names a type."
|
988 | 994 | [x]
|
|
1234 | 1240 | [x]
|
1235 | 1241 | (= 0 x))
|
1236 | 1242 |
|
| 1243 | +;;;;;;;;;;;;;;;;;;; |
| 1244 | +;; Type Coercion ;; |
| 1245 | +;;;;;;;;;;;;;;;;;;; |
| 1246 | + |
| 1247 | +(defn bigdec |
| 1248 | + "Coerce x to a Decimal." |
| 1249 | + [x] |
| 1250 | + (decimal/Decimal x)) |
| 1251 | + |
| 1252 | +(defn bigint |
| 1253 | + "Coerce x to an integer. |
| 1254 | + |
| 1255 | + Python's builtin `int` type is arbitrary precision, so there is no difference |
| 1256 | + between `bigint`, `biginteger`, and Python's builtin `int`." |
| 1257 | + [x] |
| 1258 | + (python/int x)) |
| 1259 | + |
| 1260 | +(defn biginteger |
| 1261 | + "Coerce x to an integer. |
| 1262 | + |
| 1263 | + Python's builtin `int` type is arbitrary precision, so there is no difference |
| 1264 | + between `bigint`, `biginteger`, and Python's builtin `int`." |
| 1265 | + [x] |
| 1266 | + (python/int x)) |
| 1267 | + |
| 1268 | +(defn boolean |
| 1269 | + "Coerce x to a boolean." |
| 1270 | + [x] |
| 1271 | + (python/bool x)) |
| 1272 | + |
| 1273 | +(defn byte |
| 1274 | + "Coerce x to a byte." |
| 1275 | + [x] |
| 1276 | + (let [b (python/bytes [x])] |
| 1277 | + (if (= 1 (python/len b)) |
| 1278 | + b |
| 1279 | + (throw |
| 1280 | + (python/ValueError (str "cannot coerce " (python/repr x) " to byte")))))) |
| 1281 | + |
| 1282 | +(defn boolean |
| 1283 | + "Coerce x to a boolean." |
| 1284 | + [x] |
| 1285 | + (python/bool x)) |
| 1286 | + |
| 1287 | +(defn char |
| 1288 | + "Coerce x to a string of length 1. |
| 1289 | + |
| 1290 | + Natural integers are treated as ordinals and passed to Python's `chr`. |
| 1291 | + Strings of length 1 are returned as such. Other types will result in |
| 1292 | + a `ValueError`." |
| 1293 | + [x] |
| 1294 | + (cond |
| 1295 | + (instance? python/int x) (python/chr x) |
| 1296 | + (char? x) x |
| 1297 | + :else (throw |
| 1298 | + (python/ValueError |
| 1299 | + (str "cannot cast " (python/repr x) " to char"))))) |
| 1300 | + |
| 1301 | +(defn double |
| 1302 | + "Coerce x to a float. |
| 1303 | + |
| 1304 | + Python does not differentiate between `float` and `double`. Python `float`s are |
| 1305 | + double precision." |
| 1306 | + [x] |
| 1307 | + (python/float x)) |
| 1308 | + |
| 1309 | +(defn float |
| 1310 | + "Coerce x to a float." |
| 1311 | + [x] |
| 1312 | + (python/float x)) |
| 1313 | + |
| 1314 | +(defn int |
| 1315 | + "Coerce x to an integer." |
| 1316 | + [x] |
| 1317 | + (python/int x)) |
| 1318 | + |
| 1319 | +(defn long |
| 1320 | + "Coerce x to an integer. |
| 1321 | + |
| 1322 | + Python does not support `long` types, so the value is coerced to an integer." |
| 1323 | + [x] |
| 1324 | + (python/int x)) |
| 1325 | + |
| 1326 | +(defn short |
| 1327 | + "Coerce x to an integer. |
| 1328 | + |
| 1329 | + Python does not support `short` types, so the value is coerced to an integer." |
| 1330 | + [x] |
| 1331 | + (python/int x)) |
| 1332 | + |
1237 | 1333 | ;;;;;;;;;;;;;;;;
|
1238 | 1334 | ;; Exceptions ;;
|
1239 | 1335 | ;;;;;;;;;;;;;;;;
|
|
0 commit comments