cached-expression
TypeScript icon, indicating that this package has built-in type declarations

0.0.5 • Public • Published

cached-expression

Expressions that are calculated once for each object

Usage

APIs

export declare class Calculator<X, Y> {
  public readonly calculate: (x: X) => Y
  constructor (calculate: (x: X) => Y)
  protected createCache (): MapLike<X, Y>
}
 
export default Calculator

Example

Unique Object Reference

import assert from 'assert'
import { Calculator } from 'cached-expression'
const { calculate } = new Calculator(x => [x, Math.random()])
 
const a0 = calculate('a')
const a1 = calculate('a')
const a2 = calculate('a')
const b0 = calculate('b')
 
// Same input produces same reference
assert(a0 === a1)
assert(a0 === a2)
 
// Different inputs produces different reference
assert(a0 !== b1)

Application: Factorial

Caching improve performance when calculating factorial of multiple numbers.

import { Calculator } from 'cached-expression'
 
// Definition:
//   fac(0) = 1
//   fac(n) = n * fac(n - 1)
const fac = new Calculator(
  x => x < 2
    ? 1
    : x * fac(- 1)
).calculate
 
const result = [0, 1, 2, 3, 4].map(fac)

Application: Fibonacci

Caching turn recursive fibonacci algorithm from a wasteful binary tree model to a efficient pyramid model.

import { Calculator } from 'cached-expression'
 
// Definition:
//   fib(0) = fib(1) = 1
//   fib(n) = fib(n - 2) + fib(n - 1)
const fib = new Calculator(
  x => x < 2
    ? 1
    : fib(- 2) + fib(- 1)
).calculate
 
const result = fib(7)

See Also

License

MIT © Hoàng Văn Khải

Package Sidebar

Install

npm i cached-expression

Weekly Downloads

1

Version

0.0.5

License

MIT

Unpacked Size

5.84 kB

Total Files

12

Last publish

Collaborators

  • khai96_