feat(ops): Provide return types in all of the operation-centric examples
This commit is contained in:
parent
1ee6da4294
commit
be9794fd4c
@ -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
|
* it's just for demonstration purposes
|
||||||
*/
|
*/
|
||||||
export const choose = {
|
export const choose = {
|
||||||
'NumInt,NumInt': ({factorial}) => (n,k) => Number(
|
'NumInt,NumInt': ({factorial}) => Returns(
|
||||||
factorial(n) / (factorial(k)*factorial(n-k))),
|
'NumInt', (n,k) => Number(factorial(n) / (factorial(k)*factorial(n-k)))),
|
||||||
'bigint,bigint': ({
|
'bigint,bigint': ({
|
||||||
factorial
|
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)
|
n = BigInt(n)
|
||||||
let prod = 1n
|
let prod = 1n
|
||||||
for (let i = n; i > 1n; --i) {
|
for (let i = n; i > 1n; --i) {
|
||||||
prod *= i
|
prod *= i
|
||||||
}
|
}
|
||||||
return prod
|
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
|
// OK to include a type totally not in Pocomath yet, it'll never be
|
||||||
// activated.
|
// activated.
|
||||||
// Fraction: ({quotient}) => f => quotient(f.n, f.d), // oops have that now
|
|
||||||
BigNumber: ({
|
BigNumber: ({
|
||||||
'round(BigNumber)': rnd,
|
'round(BigNumber)': rnd,
|
||||||
'equal(BigNumber,BigNumber)': eq
|
'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('isZero', 'NumInt'), 'boolean')
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
math.returnTypeOf('roundquotient', 'NumInt,number'), 'NumInt')
|
math.returnTypeOf('roundquotient', 'NumInt,number'), 'NumInt')
|
||||||
|
assert.strictEqual(
|
||||||
|
math.returnTypeOf('factorial', 'NumInt'), 'bigint')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('can subtract numbers', () => {
|
it('can subtract numbers', () => {
|
||||||
|
Loading…
Reference in New Issue
Block a user