> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/web/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/web/js/ex/exercises/pascals-triangle.md).

# Pascal's Triangle

{% tabs %}
{% tab title="demo" %}
{% embed url="<https://codesandbox.io/embed/pascal-triangle-94yoh?fontsize=14&hidenavigation=1&theme=dark>" %}
Pascal's Triangle
{% endembed %}
{% endtab %}

{% tab title="c(m,n)" %}

```javascript
import { caching } from '../decorator/caching.js';

// binomial combinations
function _c(m, n) {
  if (m - n < 0 || n < 0) return 0;
  if (m - n === 0 || n === 0) return 1;
  return _c(m - 1, n - 1) + _c(m - 1, n);
}

// give 'memory' to function '_c'
export const c = caching(_c);
```

{% endtab %}
{% endtabs %}

## caching decorator

{% tabs %}
{% tab title="caching(f)" %}

```javascript
// ⭐️ caching decorator
//    use a closure to ecapsulate 'f'
export function caching(f) {
  // make a new cache for this 'f'
  let cache = new Map();

  // returns a function that can remember
  return function (...args) {
    // if not in cache, cache it.
    const key = `${args}`;
    if (!cache.has(key)) {
      //          ╭- call a method -╮  ('f' could be a method)
      let value = f.apply(this, args); // ⭐️ with "this" context
      cache.set(key, value);
      // log(`(${key}, ${value}) cached ...`);
    }
    // get function value from cache
    return cache.get(key);
  };
}
```

{% endtab %}

{% tab title="📗 參考" %}

* JS.info ⟩ [Decorators and forwarding, call/apply](https://javascript.info/call-apply-decorators)
* PJCHENder ⟩ [\[演算法\] Fibonacci：善用 cache 和 Memoization 提升程式效能](https://pjchender.blogspot.com/2017/09/fibonacci-cache-memoization.html)
  {% endtab %}

{% tab title="💾 程式" %}

* codesandbox - [Pascal Triangle](https://codesandbox.io/s/pascal-triangle-94yoh?file=/decorator/caching.js)
* replit - [decorating methods](https://replit.com/@pegasusroe/decorating-methods#script.js)
  {% endtab %}
  {% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lochiwei.gitbook.io/web/js/ex/exercises/pascals-triangle.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
