Fuzion Logo
fuzion-lang.dev — The Fuzion Language Portal
JavaScript seems to be disabled. Functionality is limited.

Idiom # 315: Memoization

See programming-idioms.org:

Code

Let's look at two code examples that solve the same problem: How many ways are there to climb an 'n' stairs, taking 1 or 2 steps at the time?

Both versions use memoization to avoid unnecessary calculations, but they implement it in slightly different ways. You can learn about memoization effect in fuzion language from here

Running Example

Running Example

The following example ways_memo is a separate memoization element. Inside it, a function m is defined, which uses the ways_memo.env.keep method to store and retrieve results. f is a regular recursive function. This structure separates the memory (ways_memo) from the rest of the logic.

This example could be made more efficient by also memoizing the recursive calls. For this, the calculations would need to happen directly in m, without a call to f.

last changed: 2025-05-13