diff --git a/src/ops/choose.mjs b/src/ops/choose.mjs index c285dc7..dd22dc2 100644 --- a/src/ops/choose.mjs +++ b/src/ops/choose.mjs @@ -1,11 +1,13 @@ -/* Note this is not a good algorithm for computing binomial coefficients, +import Returns from '../core/Returns.mjs' + +/* Note this is _not_ a good algorithm for computing binomial coefficients, * it's just for demonstration purposes */ export const choose = { - 'NumInt,NumInt': ({factorial}) => (n,k) => Number( - factorial(n) / (factorial(k)*factorial(n-k))), + 'NumInt,NumInt': ({factorial}) => Returns( + 'NumInt', (n,k) => Number(factorial(n) / (factorial(k)*factorial(n-k)))), 'bigint,bigint': ({ factorial - }) => (n,k) => factorial(n) / (factorial(k)*factorial(n-k)) + }) => Returns('bigint', (n,k) => factorial(n) / (factorial(k)*factorial(n-k))) } diff --git a/src/ops/factorial.mjs b/src/ops/factorial.mjs index bb07047..b1154f3 100644 --- a/src/ops/factorial.mjs +++ b/src/ops/factorial.mjs @@ -1,8 +1,15 @@ -export function factorial(n) { +import Returns from '../core/Returns.mjs' + +/* Plain functions are OK, too, and they can be decorated with a return type + * just like an implementation. + */ +const factorial = Returns('bigint', function factorial(n) { n = BigInt(n) let prod = 1n for (let i = n; i > 1n; --i) { prod *= i } return prod -} +}) + +export {factorial} diff --git a/src/ops/floor.mjs b/src/ops/floor.mjs index 2fbe106..3754dcb 100644 --- a/src/ops/floor.mjs +++ b/src/ops/floor.mjs @@ -25,7 +25,6 @@ export const floor = { // OK to include a type totally not in Pocomath yet, it'll never be // activated. - // Fraction: ({quotient}) => f => quotient(f.n, f.d), // oops have that now BigNumber: ({ 'round(BigNumber)': rnd, 'equal(BigNumber,BigNumber)': eq diff --git a/test/_pocomath.mjs b/test/_pocomath.mjs index 5d80e85..c4a5c28 100644 --- a/test/_pocomath.mjs +++ b/test/_pocomath.mjs @@ -63,6 +63,8 @@ describe('The default full pocomath instance "math"', () => { assert.strictEqual(math.returnTypeOf('isZero', 'NumInt'), 'boolean') assert.strictEqual( math.returnTypeOf('roundquotient', 'NumInt,number'), 'NumInt') + assert.strictEqual( + math.returnTypeOf('factorial', 'NumInt'), 'bigint') }) it('can subtract numbers', () => {