feat(ops): Provide return types in all of the operation-centric examples

This commit is contained in:
Glen Whitney 2022-08-29 21:30:32 -04:00
parent 1ee6da4294
commit be9794fd4c
4 changed files with 17 additions and 7 deletions

View File

@ -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)))
}

View File

@ -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}

View File

@ -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

View File

@ -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', () => {