Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 15.1 — Functional Patterns
JavaScript · Example 1 of 3

Immutability: copy, don't change

Object.freeze blocked the change to theme, but the array inside was not frozen. The last line shows the mutating version handing back the very same object.

Functional patterns build programs from small functions that don't change shared data: immutability keeps values safe, composition chains functions into pipelines, and currying and partial application make specialised functions from general ones.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write updateTodo(todos, id, changes) that returns a new array where the todo with that id is merged with changes, without changing the original. Test it.

02

Write a curried filterBy(key)(value)(list) and use it to get the books by "Ruskin" from a list.

03

Use pipe to build total that takes a cart array of { price, qty }, multiplies each line, adds them up, and adds 18% tax rounded to 2 decimals.

Explain it without notes

01

What is a pure function, and why does it matter?

02

How do you update a nested property immutably, and why is spread called shallow?

03

What is the difference between currying and partial application?

04

Implement pipe and explain how it differs from compose.

Immutability: copy, don't change

Preview

Press Run to see the page.

Console

Nothing logged yet.

Expected console output

[ 'pen', 'book' ]
[ 'pen', 'book', 'lamp' ]
false false
[ 30, 10, 20 ] [ 10, 20, 30 ]
{ theme: 'dark', sizes: [ 1, 2, 3 ] }
[ 'pen', 'book', 'cup' ] true