feat: Return type annotations #53
@ -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)))
|
||||
}
|
||||
|
||||
|
@ -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}
|
||||
|
@ -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
|
||||
|
@ -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', () => {
|
||||
|
Loading…
Reference in New Issue
Block a user