| 1 | /** |
| 2 | * Works with JavaScript `bigint` values. |
| 3 | * |
| 4 | * This module exposes the native `BigInt` constructor together with helpers for |
| 5 | * checking, arithmetic, comparison, range checks, safe parsing and conversions |
| 6 | * that return `Option`, integer square roots, aggregation, ordering, |
| 7 | * equivalence, reducers, and combiners. |
| 8 | * |
| 9 | * @since 2.0.0 |
| 10 | */ |
| 11 | |
| 12 | import * as Combiner from "./Combiner.ts" |
| 13 | import * as Equ from "./Equivalence.ts" |
| 14 | import { dual } from "./Function.ts" |
| 15 | import * as Option from "./Option.ts" |
| 16 | import * as order from "./Order.ts" |
| 17 | import type { Ordering } from "./Ordering.ts" |
| 18 | import * as predicate from "./Predicate.ts" |
| 19 | import * as Reducer from "./Reducer.ts" |
| 20 | |
| 21 | /** |
| 22 | * Exposes the global bigint constructor for JavaScript bigint coercion. |
| 23 | * |
| 24 | * **When to use** |
| 25 | * |
| 26 | * Use to access native JavaScript bigint constructor coercion from the Effect |
| 27 | * module namespace. |
| 28 | * |
| 29 | * **Gotchas** |
| 30 | * |
| 31 | * This follows native `BigInt` coercion rules. It throws for invalid strings or |
| 32 | * non-integral numbers, and whitespace-only strings coerce to `0n`. |
| 33 | * |
| 34 | * @see {@link fromString} for parsing strings into an `Option` |
| 35 | * @see {@link fromNumber} for converting safe integers into an `Option` |
| 36 | * |
| 37 | * **Example** (Constructing bigints) |
| 38 | * |
| 39 | * ```ts import.meta.vitest |
| 40 | * import { BigInt } from "effect" |
| 41 | * |
| 42 | * BigInt.BigInt(123) // => 123n |
| 43 | * BigInt.BigInt("456") // => 456n |
| 44 | * ``` |
| 45 | * |
| 46 | * @category constructors |
| 47 | * @since 4.0.0 |
| 48 | */ |
| 49 | export const BigInt = globalThis.BigInt |
| 50 | |
| 51 | const bigint0 = BigInt(0) |
| 52 | const bigint1 = BigInt(1) |
| 53 | const bigint2 = BigInt(2) |
| 54 | |
| 55 | /** |
| 56 | * Checks whether a value is a `bigint`. |
| 57 | * |
| 58 | * **When to use** |
| 59 | * |
| 60 | * Use to validate unknown input and narrow it to `bigint`. |
| 61 | * |
| 62 | * **Example** (Checking for bigints) |
| 63 | * |
| 64 | * ```ts import.meta.vitest |
| 65 | * import { BigInt } from "effect" |
| 66 | * |
| 67 | * BigInt.isBigInt(1n) // => true |
| 68 | * BigInt.isBigInt(1) // => false |
| 69 | * ``` |
| 70 | * |
| 71 | * @category guards |
| 72 | * @since 2.0.0 |
| 73 | */ |
| 74 | export const isBigInt: (u: unknown) => u is bigint = predicate.isBigInt |
| 75 | |
| 76 | /** |
| 77 | * Provides an addition operation on `bigint`s. |
| 78 | * |
| 79 | * **When to use** |
| 80 | * |
| 81 | * Use when you need a binary addition function for piping or higher-order APIs |
| 82 | * instead of the infix addition operator. |
| 83 | * |
| 84 | * **Example** (Adding bigints) |
| 85 | * |
| 86 | * ```ts import.meta.vitest |
| 87 | * import { BigInt } from "effect" |
| 88 | * |
| 89 | * BigInt.sum(2n, 3n) // => 5n |
| 90 | * ``` |
| 91 | * |
| 92 | * @see {@link sumAll} for summing an iterable of `bigint` values |
| 93 | * |
| 94 | * @category math |
| 95 | * @since 2.0.0 |
| 96 | */ |
| 97 | export const sum: { |
| 98 | (that: bigint): (self: bigint) => bigint |
| 99 | (self: bigint, that: bigint): bigint |
| 100 | } = dual(2, (self: bigint, that: bigint): bigint => self + that) |
| 101 | |
| 102 | /** |
| 103 | * Provides a multiplication operation on `bigint`s. |
| 104 | * |
| 105 | * **When to use** |
| 106 | * |
| 107 | * Use to multiply two `bigint` values. |
| 108 | * |
| 109 | * **Example** (Multiplying bigints) |
| 110 | * |
| 111 | * ```ts import.meta.vitest |
| 112 | * import { BigInt } from "effect" |
| 113 | * |
| 114 | * BigInt.multiply(2n, 3n) // => 6n |
| 115 | * ``` |
| 116 | * |
| 117 | * @see {@link multiplyAll} for multiplying an iterable of `bigint` values |
| 118 | * |
| 119 | * @category math |
| 120 | * @since 2.0.0 |
| 121 | */ |
| 122 | export const multiply: { |
| 123 | (that: bigint): (self: bigint) => bigint |
| 124 | (self: bigint, that: bigint): bigint |
| 125 | } = dual(2, (self: bigint, that: bigint): bigint => self * that) |
| 126 | |
| 127 | /** |
| 128 | * Provides a subtraction operation on `bigint`s. |
| 129 | * |
| 130 | * **When to use** |
| 131 | * |
| 132 | * Use to subtract one `bigint` value from another. |
| 133 | * |
| 134 | * **Example** (Subtracting bigints) |
| 135 | * |
| 136 | * ```ts import.meta.vitest |
| 137 | * import { BigInt } from "effect" |
| 138 | * |
| 139 | * BigInt.subtract(2n, 3n) // => -1n |
| 140 | * ``` |
| 141 | * |
| 142 | * @category math |
| 143 | * @since 2.0.0 |
| 144 | */ |
| 145 | export const subtract: { |
| 146 | (that: bigint): (self: bigint) => bigint |
| 147 | (self: bigint, that: bigint): bigint |
| 148 | } = dual(2, (self: bigint, that: bigint): bigint => self - that) |
| 149 | |
| 150 | /** |
| 151 | * Divides one `bigint` by another safely. |
| 152 | * |
| 153 | * **When to use** |
| 154 | * |
| 155 | * Use to divide `bigint` values while representing division by zero as |
| 156 | * `Option.none`. |
| 157 | * |
| 158 | * **Details** |
| 159 | * |
| 160 | * Uses JavaScript `bigint` division, so non-exact quotients are truncated |
| 161 | * toward zero. Returns `Option.none()` when the divisor is `0n`. |
| 162 | * |
| 163 | * **Example** (Dividing bigints safely) |
| 164 | * |
| 165 | * ```ts import.meta.vitest |
| 166 | * import { BigInt, Option } from "effect" |
| 167 | * |
| 168 | * BigInt.divide(6n, 3n) // => Option.some(2n) |
| 169 | * BigInt.divide(6n, 0n) // => Option.none() |
| 170 | * ``` |
| 171 | * |
| 172 | * @see {@link divideUnsafe} for division that throws when the divisor is `0n` |
| 173 | * @see {@link remainder} for the JavaScript remainder operation |
| 174 | * |
| 175 | * @category math |
| 176 | * @since 2.0.0 |
| 177 | */ |
| 178 | export const divide: { |
| 179 | (that: bigint): (self: bigint) => Option.Option<bigint> |
| 180 | (self: bigint, that: bigint): Option.Option<bigint> |
| 181 | } = dual( |
| 182 | 2, |
| 183 | (self: bigint, that: bigint): Option.Option<bigint> => that === bigint0 ? Option.none() : Option.some(self / that) |
| 184 | ) |
| 185 | |
| 186 | /** |
| 187 | * Divides one `bigint` by another, throwing if the divisor is zero. |
| 188 | * |
| 189 | * **When to use** |
| 190 | * |
| 191 | * Use to divide `bigint` values where the divisor is known to be non-zero and |
| 192 | * division by zero should be a thrown exception. |
| 193 | * |
| 194 | * **Details** |
| 195 | * |
| 196 | * Uses JavaScript `bigint` division, so non-exact quotients are truncated |
| 197 | * toward zero. |
| 198 | * |
| 199 | * **Gotchas** |
| 200 | * |
| 201 | * Throws a `RangeError` when the divisor is `0n`. |
| 202 | * |
| 203 | * **Example** (Dividing bigints unsafely) |
| 204 | * |
| 205 | * ```ts import.meta.vitest |
| 206 | * import { BigInt } from "effect" |
| 207 | * |
| 208 | * BigInt.divideUnsafe(6n, 3n) // => 2n |
| 209 | * BigInt.divideUnsafe(6n, 4n) // => 1n |
| 210 | * ``` |
| 211 | * |
| 212 | * @see {@link divide} for division that returns `Option.none` when the divisor is `0n` |
| 213 | * |
| 214 | * @category math |
| 215 | * @since 4.0.0 |
| 216 | */ |
| 217 | export const divideUnsafe: { |
| 218 | (that: bigint): (self: bigint) => bigint |
| 219 | (self: bigint, that: bigint): bigint |
| 220 | } = dual(2, (self: bigint, that: bigint): bigint => self / that) |
| 221 | |
| 222 | /** |
| 223 | * Returns the result of adding `1n` to a `bigint`. |
| 224 | * |
| 225 | * **When to use** |
| 226 | * |
| 227 | * Use to increment a `bigint` counter by one. |
| 228 | * |
| 229 | * **Example** (Incrementing a bigint) |
| 230 | * |
| 231 | * ```ts import.meta.vitest |
| 232 | * import { BigInt } from "effect" |
| 233 | * |
| 234 | * BigInt.increment(2n) // => 3n |
| 235 | * ``` |
| 236 | * |
| 237 | * @category math |
| 238 | * @since 2.0.0 |
| 239 | */ |
| 240 | export const increment = (n: bigint): bigint => n + bigint1 |
| 241 | |
| 242 | /** |
| 243 | * Returns the result of subtracting `1n` from a `bigint`. |
| 244 | * |
| 245 | * **When to use** |
| 246 | * |
| 247 | * Use to decrement a `bigint` counter by one. |
| 248 | * |
| 249 | * **Example** (Decrementing a bigint) |
| 250 | * |
| 251 | * ```ts import.meta.vitest |
| 252 | * import { BigInt } from "effect" |
| 253 | * |
| 254 | * BigInt.decrement(3n) // => 2n |
| 255 | * ``` |
| 256 | * |
| 257 | * @category math |
| 258 | * @since 2.0.0 |
| 259 | */ |
| 260 | export const decrement = (n: bigint): bigint => n - bigint1 |
| 261 | |
| 262 | /** |
| 263 | * Provides an `Order` instance for `bigint` that allows comparing and sorting BigInt values. |
| 264 | * |
| 265 | * **When to use** |
| 266 | * |
| 267 | * Use when you need to sort or compare bigint values through APIs that accept |
| 268 | * an ordering instance. |
| 269 | * |
| 270 | * **Example** (Comparing bigints with Order) |
| 271 | * |
| 272 | * ```ts import.meta.vitest |
| 273 | * import { BigInt } from "effect" |
| 274 | * |
| 275 | * const a = 123n |
| 276 | * const b = 456n |
| 277 | * const c = 123n |
| 278 | * |
| 279 | * BigInt.Order(a, b) // => -1 |
| 280 | * BigInt.Order(b, a) // => 1 |
| 281 | * BigInt.Order(a, c) // => 0 |
| 282 | * ``` |
| 283 | * |
| 284 | * @category instances |
| 285 | * @since 2.0.0 |
| 286 | */ |
| 287 | export const Order: order.Order<bigint> = order.BigInt |
| 288 | |
| 289 | /** |
| 290 | * Equivalence instance for bigints using strict equality (`===`). |
| 291 | * |
| 292 | * **When to use** |
| 293 | * |
| 294 | * Use when checking bigint equality through APIs that accept an equivalence |
| 295 | * relation. |
| 296 | * |
| 297 | * **Example** (Comparing bigints for equivalence) |
| 298 | * |
| 299 | * ```ts import.meta.vitest |
| 300 | * import { BigInt } from "effect" |
| 301 | * |
| 302 | * BigInt.Equivalence(1n, 1n) // => true |
| 303 | * BigInt.Equivalence(1n, 2n) // => false |
| 304 | * ``` |
| 305 | * |
| 306 | * @category instances |
| 307 | * @since 2.0.0 |
| 308 | */ |
| 309 | export const Equivalence: Equ.Equivalence<bigint> = Equ.BigInt |
| 310 | |
| 311 | /** |
| 312 | * Returns `true` if the first argument is less than the second, otherwise `false`. |
| 313 | * |
| 314 | * **When to use** |
| 315 | * |
| 316 | * Use to test whether one `bigint` is strictly less than another. |
| 317 | * |
| 318 | * **Example** (Checking less-than comparisons) |
| 319 | * |
| 320 | * ```ts import.meta.vitest |
| 321 | * import { BigInt } from "effect" |
| 322 | * |
| 323 | * BigInt.isLessThan(2n, 3n) // => true |
| 324 | * BigInt.isLessThan(3n, 3n) // => false |
| 325 | * BigInt.isLessThan(4n, 3n) // => false |
| 326 | * ``` |
| 327 | * |
| 328 | * @category predicates |
| 329 | * @since 4.0.0 |
| 330 | */ |
| 331 | export const isLessThan: { |
| 332 | (that: bigint): (self: bigint) => boolean |
| 333 | (self: bigint, that: bigint): boolean |
| 334 | } = order.isLessThan(Order) |
| 335 | |
| 336 | /** |
| 337 | * Returns a function that checks if a given `bigint` is less than or equal to the provided one. |
| 338 | * |
| 339 | * **When to use** |
| 340 | * |
| 341 | * Use to test whether one `bigint` is less than or equal to another. |
| 342 | * |
| 343 | * **Example** (Checking less-than-or-equal comparisons) |
| 344 | * |
| 345 | * ```ts import.meta.vitest |
| 346 | * import { BigInt } from "effect" |
| 347 | * |
| 348 | * BigInt.isLessThanOrEqualTo(2n, 3n) // => true |
| 349 | * BigInt.isLessThanOrEqualTo(3n, 3n) // => true |
| 350 | * BigInt.isLessThanOrEqualTo(4n, 3n) // => false |
| 351 | * ``` |
| 352 | * |
| 353 | * @category predicates |
| 354 | * @since 4.0.0 |
| 355 | */ |
| 356 | export const isLessThanOrEqualTo: { |
| 357 | (that: bigint): (self: bigint) => boolean |
| 358 | (self: bigint, that: bigint): boolean |
| 359 | } = order.isLessThanOrEqualTo(Order) |
| 360 | |
| 361 | /** |
| 362 | * Returns `true` if the first argument is greater than the second, otherwise `false`. |
| 363 | * |
| 364 | * **When to use** |
| 365 | * |
| 366 | * Use to test whether one `bigint` is strictly greater than another. |
| 367 | * |
| 368 | * **Example** (Checking greater-than comparisons) |
| 369 | * |
| 370 | * ```ts import.meta.vitest |
| 371 | * import { BigInt } from "effect" |
| 372 | * |
| 373 | * BigInt.isGreaterThan(2n, 3n) // => false |
| 374 | * BigInt.isGreaterThan(3n, 3n) // => false |
| 375 | * BigInt.isGreaterThan(4n, 3n) // => true |
| 376 | * ``` |
| 377 | * |
| 378 | * @category predicates |
| 379 | * @since 4.0.0 |
| 380 | */ |
| 381 | export const isGreaterThan: { |
| 382 | (that: bigint): (self: bigint) => boolean |
| 383 | (self: bigint, that: bigint): boolean |
| 384 | } = order.isGreaterThan(Order) |
| 385 | |
| 386 | /** |
| 387 | * Returns a function that checks if a given `bigint` is greater than or equal to the provided one. |
| 388 | * |
| 389 | * **When to use** |
| 390 | * |
| 391 | * Use to test whether one `bigint` is greater than or equal to another. |
| 392 | * |
| 393 | * **Example** (Checking greater-than-or-equal comparisons) |
| 394 | * |
| 395 | * ```ts import.meta.vitest |
| 396 | * import { BigInt } from "effect" |
| 397 | * |
| 398 | * BigInt.isGreaterThanOrEqualTo(2n, 3n) // => false |
| 399 | * BigInt.isGreaterThanOrEqualTo(3n, 3n) // => true |
| 400 | * BigInt.isGreaterThanOrEqualTo(4n, 3n) // => true |
| 401 | * ``` |
| 402 | * |
| 403 | * @category predicates |
| 404 | * @since 4.0.0 |
| 405 | */ |
| 406 | export const isGreaterThanOrEqualTo: { |
| 407 | (that: bigint): (self: bigint) => boolean |
| 408 | (self: bigint, that: bigint): boolean |
| 409 | } = order.isGreaterThanOrEqualTo(Order) |
| 410 | |
| 411 | /** |
| 412 | * Checks whether a `bigint` is between a `minimum` and `maximum` value (inclusive). |
| 413 | * |
| 414 | * **When to use** |
| 415 | * |
| 416 | * Use to test whether a `bigint` falls inside an inclusive range. |
| 417 | * |
| 418 | * **Example** (Checking whether a bigint is within bounds) |
| 419 | * |
| 420 | * ```ts import.meta.vitest |
| 421 | * import { BigInt } from "effect" |
| 422 | * |
| 423 | * const between = BigInt.between({ minimum: 0n, maximum: 5n }) |
| 424 | * |
| 425 | * between(3n) // => true |
| 426 | * between(-1n) // => false |
| 427 | * between(6n) // => false |
| 428 | * ``` |
| 429 | * |
| 430 | * @see {@link clamp} for forcing a `bigint` into an inclusive range |
| 431 | * |
| 432 | * @category predicates |
| 433 | * @since 2.0.0 |
| 434 | */ |
| 435 | export const between: { |
| 436 | (options: { |
| 437 | minimum: bigint |
| 438 | maximum: bigint |
| 439 | }): (self: bigint) => boolean |
| 440 | (self: bigint, options: { |
| 441 | minimum: bigint |
| 442 | maximum: bigint |
| 443 | }): boolean |
| 444 | } = order.isBetween(Order) |
| 445 | |
| 446 | /** |
| 447 | * Restricts the given `bigint` to be within the range specified by the `minimum` and `maximum` values. |
| 448 | * |
| 449 | * **When to use** |
| 450 | * |
| 451 | * Use to force a `bigint` into an inclusive range. |
| 452 | * |
| 453 | * **Details** |
| 454 | * |
| 455 | * If the `bigint` is less than the minimum, the function returns the minimum. |
| 456 | * If the `bigint` is greater than the maximum, the function returns the |
| 457 | * maximum. Otherwise, it returns the original `bigint`. |
| 458 | * |
| 459 | * **Example** (Clamping a bigint to bounds) |
| 460 | * |
| 461 | * ```ts import.meta.vitest |
| 462 | * import { BigInt } from "effect" |
| 463 | * |
| 464 | * const clamp = BigInt.clamp({ minimum: 1n, maximum: 5n }) |
| 465 | * |
| 466 | * clamp(3n) // => 3n |
| 467 | * clamp(0n) // => 1n |
| 468 | * clamp(6n) // => 5n |
| 469 | * ``` |
| 470 | * |
| 471 | * @see {@link between} for checking whether a `bigint` is already inside a range |
| 472 | * |
| 473 | * @category math |
| 474 | * @since 2.0.0 |
| 475 | */ |
| 476 | export const clamp: { |
| 477 | (options: { |
| 478 | minimum: bigint |
| 479 | maximum: bigint |
| 480 | }): (self: bigint) => bigint |
| 481 | (self: bigint, options: { |
| 482 | minimum: bigint |
| 483 | maximum: bigint |
| 484 | }): bigint |
| 485 | } = order.clamp(Order) |
| 486 | |
| 487 | /** |
| 488 | * Returns the minimum between two `bigint`s. |
| 489 | * |
| 490 | * **When to use** |
| 491 | * |
| 492 | * Use to select the smaller of two `bigint` values. |
| 493 | * |
| 494 | * **Example** (Finding the minimum bigint) |
| 495 | * |
| 496 | * ```ts import.meta.vitest |
| 497 | * import { BigInt } from "effect" |
| 498 | * |
| 499 | * BigInt.min(2n, 3n) // => 2n |
| 500 | * ``` |
| 501 | * |
| 502 | * @see {@link max} for selecting the larger value |
| 503 | * |
| 504 | * @category math |
| 505 | * @since 2.0.0 |
| 506 | */ |
| 507 | export const min: { |
| 508 | (that: bigint): (self: bigint) => bigint |
| 509 | (self: bigint, that: bigint): bigint |
| 510 | } = order.min(Order) |
| 511 | |
| 512 | /** |
| 513 | * Returns the maximum between two `bigint`s. |
| 514 | * |
| 515 | * **When to use** |
| 516 | * |
| 517 | * Use to select the larger of two `bigint` values. |
| 518 | * |
| 519 | * **Example** (Finding the maximum bigint) |
| 520 | * |
| 521 | * ```ts import.meta.vitest |
| 522 | * import { BigInt } from "effect" |
| 523 | * |
| 524 | * BigInt.max(2n, 3n) // => 3n |
| 525 | * ``` |
| 526 | * |
| 527 | * @see {@link min} for selecting the smaller value |
| 528 | * |
| 529 | * @category math |
| 530 | * @since 2.0.0 |
| 531 | */ |
| 532 | export const max: { |
| 533 | (that: bigint): (self: bigint) => bigint |
| 534 | (self: bigint, that: bigint): bigint |
| 535 | } = order.max(Order) |
| 536 | |
| 537 | /** |
| 538 | * Determines the sign of a given `bigint`. |
| 539 | * |
| 540 | * **When to use** |
| 541 | * |
| 542 | * Use to classify a `bigint` as negative, zero, or positive. |
| 543 | * |
| 544 | * **Example** (Determining bigint signs) |
| 545 | * |
| 546 | * ```ts import.meta.vitest |
| 547 | * import { BigInt } from "effect" |
| 548 | * |
| 549 | * BigInt.sign(-5n) // => -1 |
| 550 | * BigInt.sign(0n) // => 0 |
| 551 | * BigInt.sign(5n) // => 1 |
| 552 | * ``` |
| 553 | * |
| 554 | * @category math |
| 555 | * @since 2.0.0 |
| 556 | */ |
| 557 | export const sign = (n: bigint): Ordering => order.BigInt(n, bigint0) |
| 558 | |
| 559 | /** |
| 560 | * Determines the absolute value of a given `bigint`. |
| 561 | * |
| 562 | * **When to use** |
| 563 | * |
| 564 | * Use to remove the sign from a `bigint` while preserving its magnitude. |
| 565 | * |
| 566 | * **Example** (Calculating absolute values) |
| 567 | * |
| 568 | * ```ts import.meta.vitest |
| 569 | * import { BigInt } from "effect" |
| 570 | * |
| 571 | * BigInt.abs(-5n) // => 5n |
| 572 | * BigInt.abs(0n) // => 0n |
| 573 | * BigInt.abs(5n) // => 5n |
| 574 | * ``` |
| 575 | * |
| 576 | * @category math |
| 577 | * @since 2.0.0 |
| 578 | */ |
| 579 | export const abs = (n: bigint): bigint => (n < bigint0 ? -n : n) |
| 580 | |
| 581 | /** |
| 582 | * Determines the greatest common divisor of two `bigint`s. |
| 583 | * |
| 584 | * **When to use** |
| 585 | * |
| 586 | * Use to compute the greatest common divisor of two integer values. |
| 587 | * |
| 588 | * **Example** (Calculating greatest common divisors) |
| 589 | * |
| 590 | * ```ts import.meta.vitest |
| 591 | * import { BigInt } from "effect" |
| 592 | * |
| 593 | * BigInt.gcd(2n, 3n) // => 1n |
| 594 | * BigInt.gcd(2n, 4n) // => 2n |
| 595 | * BigInt.gcd(16n, 24n) // => 8n |
| 596 | * ``` |
| 597 | * |
| 598 | * @see {@link lcm} for computing the least common multiple |
| 599 | * |
| 600 | * @category math |
| 601 | * @since 2.0.0 |
| 602 | */ |
| 603 | export const gcd: { |
| 604 | (that: bigint): (self: bigint) => bigint |
| 605 | (self: bigint, that: bigint): bigint |
| 606 | } = dual(2, (self: bigint, that: bigint): bigint => { |
| 607 | while (that !== bigint0) { |
| 608 | const t = that |
| 609 | that = self % that |
| 610 | self = t |
| 611 | } |
| 612 | return abs(self) |
| 613 | }) |
| 614 | |
| 615 | /** |
| 616 | * Determines the least common multiple of two `bigint`s. |
| 617 | * |
| 618 | * **When to use** |
| 619 | * |
| 620 | * Use to compute the least common multiple of two integer values. |
| 621 | * |
| 622 | * **Example** (Calculating least common multiples) |
| 623 | * |
| 624 | * ```ts import.meta.vitest |
| 625 | * import { BigInt } from "effect" |
| 626 | * |
| 627 | * BigInt.lcm(2n, 3n) // => 6n |
| 628 | * BigInt.lcm(2n, 4n) // => 4n |
| 629 | * BigInt.lcm(16n, 24n) // => 48n |
| 630 | * ``` |
| 631 | * |
| 632 | * @see {@link gcd} for computing the greatest common divisor |
| 633 | * |
| 634 | * @category math |
| 635 | * @since 2.0.0 |
| 636 | */ |
| 637 | export const lcm: { |
| 638 | (that: bigint): (self: bigint) => bigint |
| 639 | (self: bigint, that: bigint): bigint |
| 640 | } = dual( |
| 641 | 2, |
| 642 | (self: bigint, that: bigint): bigint => |
| 643 | self === bigint0 || that === bigint0 ? bigint0 : abs((self * that) / gcd(self, that)) |
| 644 | ) |
| 645 | |
| 646 | /** |
| 647 | * Returns the integer square root of a non-negative `bigint`. |
| 648 | * |
| 649 | * **When to use** |
| 650 | * |
| 651 | * Use when you need to compute an integer square root for a `bigint` that has |
| 652 | * already been validated as non-negative, and you want negative input to throw |
| 653 | * instead of returning `Option.none`. |
| 654 | * |
| 655 | * **Details** |
| 656 | * |
| 657 | * For non-perfect squares, returns the largest `bigint` whose square is less |
| 658 | * than or equal to the input. |
| 659 | * |
| 660 | * **Gotchas** |
| 661 | * |
| 662 | * Throws a `RangeError` if the input is negative. |
| 663 | * |
| 664 | * **Example** (Calculating square roots unsafely) |
| 665 | * |
| 666 | * ```ts import.meta.vitest |
| 667 | * import { BigInt } from "effect" |
| 668 | * |
| 669 | * BigInt.sqrtUnsafe(4n) // => 2n |
| 670 | * BigInt.sqrtUnsafe(9n) // => 3n |
| 671 | * BigInt.sqrtUnsafe(16n) // => 4n |
| 672 | * ``` |
| 673 | * |
| 674 | * @see {@link sqrt} for returning `Option.none` when the input is negative |
| 675 | * |
| 676 | * @category math |
| 677 | * @since 4.0.0 |
| 678 | */ |
| 679 | export const sqrtUnsafe = (n: bigint): bigint => { |
| 680 | if (n < bigint0) { |
| 681 | throw new RangeError("Cannot take the square root of a negative number") |
| 682 | } |
| 683 | if (n < bigint2) { |
| 684 | return n |
| 685 | } |
| 686 | let x = n / bigint2 |
| 687 | while (x * x > n) { |
| 688 | x = ((n / x) + x) / bigint2 |
| 689 | } |
| 690 | return x |
| 691 | } |
| 692 | |
| 693 | /** |
| 694 | * Computes the integer square root of a `bigint` safely. |
| 695 | * |
| 696 | * **When to use** |
| 697 | * |
| 698 | * Use to compute an integer square root while representing negative input as |
| 699 | * `Option.none`. |
| 700 | * |
| 701 | * **Details** |
| 702 | * |
| 703 | * For non-perfect squares, returns the largest `bigint` whose square is less |
| 704 | * than or equal to the input. Returns `Option.none()` when the input is |
| 705 | * negative. |
| 706 | * |
| 707 | * **Example** (Calculating square roots safely) |
| 708 | * |
| 709 | * ```ts import.meta.vitest |
| 710 | * import { BigInt, Option } from "effect" |
| 711 | * |
| 712 | * BigInt.sqrt(4n) // => Option.some(2n) |
| 713 | * BigInt.sqrt(9n) // => Option.some(3n) |
| 714 | * BigInt.sqrt(16n) // => Option.some(4n) |
| 715 | * BigInt.sqrt(-1n) // => Option.none() |
| 716 | * ``` |
| 717 | * |
| 718 | * @see {@link sqrtUnsafe} for square root computation that throws on negative input |
| 719 | * |
| 720 | * @category math |
| 721 | * @since 2.0.0 |
| 722 | */ |
| 723 | export const sqrt = (n: bigint): Option.Option<bigint> => |
| 724 | isGreaterThanOrEqualTo(n, bigint0) ? Option.some(sqrtUnsafe(n)) : Option.none() |
| 725 | |
| 726 | /** |
| 727 | * Takes an `Iterable` of `bigint`s and returns their sum as a single `bigint`. Returns `0n` for an empty iterable. |
| 728 | * |
| 729 | * **When to use** |
| 730 | * |
| 731 | * Use when you want an immediate aggregate from an iterable instead of a |
| 732 | * folding reducer owned by another API. |
| 733 | * |
| 734 | * **Example** (Summing iterable bigints) |
| 735 | * |
| 736 | * ```ts import.meta.vitest |
| 737 | * import { BigInt } from "effect" |
| 738 | * |
| 739 | * BigInt.sumAll([2n, 3n, 4n]) // => 9n |
| 740 | * ``` |
| 741 | * |
| 742 | * @see {@link sum} for adding two `bigint` values |
| 743 | * @see {@link ReducerSum} for summing through APIs that consume a `Reducer` |
| 744 | * |
| 745 | * @category math |
| 746 | * @since 2.0.0 |
| 747 | */ |
| 748 | export const sumAll = (collection: Iterable<bigint>): bigint => { |
| 749 | let out = bigint0 |
| 750 | for (const n of collection) { |
| 751 | out += n |
| 752 | } |
| 753 | return out |
| 754 | } |
| 755 | |
| 756 | /** |
| 757 | * Takes an `Iterable` of `bigint`s and returns their product as a single `bigint`. Returns `1n` for an empty iterable. |
| 758 | * |
| 759 | * **When to use** |
| 760 | * |
| 761 | * Use to multiply all `bigint` values in an iterable. |
| 762 | * |
| 763 | * **Example** (Multiplying iterable bigints) |
| 764 | * |
| 765 | * ```ts import.meta.vitest |
| 766 | * import { BigInt } from "effect" |
| 767 | * |
| 768 | * BigInt.multiplyAll([2n, 3n, 4n]) // => 24n |
| 769 | * ``` |
| 770 | * |
| 771 | * @see {@link multiply} for multiplying two `bigint` values |
| 772 | * @see {@link ReducerMultiply} for multiplying through APIs that consume a `Reducer` |
| 773 | * |
| 774 | * @category math |
| 775 | * @since 2.0.0 |
| 776 | */ |
| 777 | export const multiplyAll = (collection: Iterable<bigint>): bigint => { |
| 778 | let out = bigint1 |
| 779 | for (const n of collection) { |
| 780 | if (n === bigint0) { |
| 781 | return bigint0 |
| 782 | } |
| 783 | out *= n |
| 784 | } |
| 785 | return out |
| 786 | } |
| 787 | |
| 788 | /** |
| 789 | * Converts a `bigint` to a `number` safely. |
| 790 | * |
| 791 | * **When to use** |
| 792 | * |
| 793 | * Use to convert a `bigint` to a JavaScript number only when it is a safe |
| 794 | * integer. |
| 795 | * |
| 796 | * **Details** |
| 797 | * |
| 798 | * If the `bigint` is outside the safe integer range for JavaScript (`Number.MAX_SAFE_INTEGER` |
| 799 | * and `Number.MIN_SAFE_INTEGER`), it returns `Option.none()`. |
| 800 | * |
| 801 | * **Example** (Converting bigints to numbers) |
| 802 | * |
| 803 | * ```ts import.meta.vitest |
| 804 | * import { BigInt as BI, Option } from "effect" |
| 805 | * |
| 806 | * BI.toNumber(42n) // => Option.some(42) |
| 807 | * BI.toNumber(9007199254740992n) // => Option.none() |
| 808 | * BI.toNumber(-9007199254740992n) // => Option.none() |
| 809 | * ``` |
| 810 | * |
| 811 | * @see {@link fromNumber} for converting a safe integer number to `bigint` |
| 812 | * |
| 813 | * @category converting |
| 814 | * @since 2.0.0 |
| 815 | */ |
| 816 | export const toNumber = (b: bigint): Option.Option<number> => { |
| 817 | if (b > BigInt(Number.MAX_SAFE_INTEGER) || b < BigInt(Number.MIN_SAFE_INTEGER)) { |
| 818 | return Option.none() |
| 819 | } |
| 820 | return Option.some(Number(b)) |
| 821 | } |
| 822 | |
| 823 | /** |
| 824 | * Parses a string into a `bigint` safely. |
| 825 | * |
| 826 | * **When to use** |
| 827 | * |
| 828 | * Use to parse a string as a `bigint` without throwing on invalid input. |
| 829 | * |
| 830 | * **Details** |
| 831 | * |
| 832 | * If the string is empty or contains characters that cannot be converted into a |
| 833 | * `bigint`, it returns `Option.none()`. |
| 834 | * |
| 835 | * **Example** (Parsing strings as bigints) |
| 836 | * |
| 837 | * ```ts import.meta.vitest |
| 838 | * import { BigInt, Option } from "effect" |
| 839 | * |
| 840 | * BigInt.fromString("42") // => Option.some(42n) |
| 841 | * BigInt.fromString(" ") // => Option.none() |
| 842 | * BigInt.fromString("a") // => Option.none() |
| 843 | * ``` |
| 844 | * |
| 845 | * @see {@link BigInt} for native constructor coercion that throws on invalid input |
| 846 | * |
| 847 | * @category converting |
| 848 | * @since 2.4.12 |
| 849 | */ |
| 850 | export const fromString = (s: string): Option.Option<bigint> => { |
| 851 | try { |
| 852 | return s.trim() === "" |
| 853 | ? Option.none() |
| 854 | : Option.some(BigInt(s)) |
| 855 | } catch { |
| 856 | return Option.none() |
| 857 | } |
| 858 | } |
| 859 | |
| 860 | /** |
| 861 | * Converts a number to a `bigint`. |
| 862 | * |
| 863 | * **When to use** |
| 864 | * |
| 865 | * Use to convert a JavaScript number to `bigint` only when it is a safe integer. |
| 866 | * |
| 867 | * **Details** |
| 868 | * |
| 869 | * If the number is outside the safe integer range for JavaScript |
| 870 | * (`Number.MAX_SAFE_INTEGER` and `Number.MIN_SAFE_INTEGER`) or if the number is |
| 871 | * not a valid `bigint`, it returns `Option.none()`. |
| 872 | * |
| 873 | * **Example** (Converting numbers to bigints) |
| 874 | * |
| 875 | * ```ts import.meta.vitest |
| 876 | * import { BigInt, Option } from "effect" |
| 877 | * |
| 878 | * BigInt.fromNumber(42) // => Option.some(42n) |
| 879 | * BigInt.fromNumber(Number.MAX_SAFE_INTEGER + 1) // => Option.none() |
| 880 | * BigInt.fromNumber(Number.MIN_SAFE_INTEGER - 1) // => Option.none() |
| 881 | * ``` |
| 882 | * |
| 883 | * @see {@link toNumber} for converting `bigint` values back to safe integer numbers |
| 884 | * @see {@link BigInt} for native constructor coercion |
| 885 | * |
| 886 | * @category converting |
| 887 | * @since 2.4.12 |
| 888 | */ |
| 889 | export function fromNumber(n: number): Option.Option<bigint> { |
| 890 | if (n > Number.MAX_SAFE_INTEGER || n < Number.MIN_SAFE_INTEGER) { |
| 891 | return Option.none() |
| 892 | } |
| 893 | |
| 894 | try { |
| 895 | return Option.some(BigInt(n)) |
| 896 | } catch { |
| 897 | return Option.none() |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | /** |
| 902 | * Returns the JavaScript remainder of dividing one `bigint` by another. |
| 903 | * |
| 904 | * **When to use** |
| 905 | * |
| 906 | * Use when you want native remainder semantics, including signed remainders and |
| 907 | * a thrown division-by-zero error. |
| 908 | * |
| 909 | * **Gotchas** |
| 910 | * |
| 911 | * Throws a `RangeError` when the divisor is `0n`. |
| 912 | * |
| 913 | * **Example** (Calculating remainders) |
| 914 | * |
| 915 | * ```ts import.meta.vitest |
| 916 | * import { BigInt } from "effect" |
| 917 | * |
| 918 | * BigInt.remainder(10n, 3n) // => 1n |
| 919 | * BigInt.remainder(15n, 4n) // => 3n |
| 920 | * ``` |
| 921 | * |
| 922 | * @see {@link divide} for quotient calculation with division-by-zero represented as `Option.none` |
| 923 | * |
| 924 | * @category math |
| 925 | * @since 4.0.0 |
| 926 | */ |
| 927 | export const remainder: { |
| 928 | (divisor: bigint): (self: bigint) => bigint |
| 929 | (self: bigint, divisor: bigint): bigint |
| 930 | } = dual(2, (self: bigint, divisor: bigint): bigint => self % divisor) |
| 931 | |
| 932 | /** |
| 933 | * Reducer for combining `bigint`s using addition. |
| 934 | * |
| 935 | * **When to use** |
| 936 | * |
| 937 | * Use to sum many `bigint` values through APIs that consume a `Reducer`. |
| 938 | * |
| 939 | * **Details** |
| 940 | * |
| 941 | * The initial value is `0n`, so `combineAll([])` returns `0n`. |
| 942 | * |
| 943 | * @see {@link sumAll} for summing an iterable directly |
| 944 | * @see {@link ReducerMultiply} for multiplying `bigint` values |
| 945 | * |
| 946 | * @category math |
| 947 | * @since 4.0.0 |
| 948 | */ |
| 949 | export const ReducerSum: Reducer.Reducer<bigint> = Reducer.make((a, b) => a + b, bigint0) |
| 950 | |
| 951 | /** |
| 952 | * Reducer for combining `bigint`s using multiplication. |
| 953 | * |
| 954 | * **When to use** |
| 955 | * |
| 956 | * Use to multiply many `bigint` values through APIs that consume a `Reducer`. |
| 957 | * |
| 958 | * **Details** |
| 959 | * |
| 960 | * The initial value is `1n`, so `combineAll([])` returns `1n`. |
| 961 | * |
| 962 | * @see {@link multiplyAll} for multiplying an iterable directly |
| 963 | * @see {@link ReducerSum} for summing `bigint` values |
| 964 | * |
| 965 | * @category math |
| 966 | * @since 4.0.0 |
| 967 | */ |
| 968 | export const ReducerMultiply: Reducer.Reducer<bigint> = Reducer.make((a, b) => a * b, bigint1, (collection) => { |
| 969 | let acc = bigint1 |
| 970 | for (const n of collection) { |
| 971 | if (n === bigint0) return bigint0 |
| 972 | acc *= n |
| 973 | } |
| 974 | return acc |
| 975 | }) |
| 976 | |
| 977 | /** |
| 978 | * Combiner that returns the maximum `bigint`. |
| 979 | * |
| 980 | * **When to use** |
| 981 | * |
| 982 | * Use to keep the largest `bigint` when an API consumes a `Combiner`. |
| 983 | * |
| 984 | * @see {@link CombinerMin} for keeping the smallest `bigint` |
| 985 | * @see {@link max} for comparing two `bigint` values directly |
| 986 | * |
| 987 | * @category math |
| 988 | * @since 4.0.0 |
| 989 | */ |
| 990 | export const CombinerMax: Combiner.Combiner<bigint> = Combiner.max(Order) |
| 991 | |
| 992 | /** |
| 993 | * Combiner that returns the minimum `bigint`. |
| 994 | * |
| 995 | * **When to use** |
| 996 | * |
| 997 | * Use to keep the smallest `bigint` through APIs that consume a `Combiner`. |
| 998 | * |
| 999 | * @see {@link CombinerMax} for keeping the largest `bigint` |
| 1000 | * @see {@link min} for comparing two `bigint` values directly |
| 1001 | * |
| 1002 | * @category math |
| 1003 | * @since 4.0.0 |
| 1004 | */ |
| 1005 | export const CombinerMin: Combiner.Combiner<bigint> = Combiner.min(Order) |