liveBook · Manning

Balance

As explained in The Joy of JavaScript

The amount of value associated with an account or wallet after transactions are accounted for.

A balance is the value associated with a wallet after its ledger transactions are accounted for. Computing it means ignoring the genesis block, examining each transaction, and combining incoming and outgoing funds. section 4.7

Two implementations expose different design tradeoffs:

  • An imperative algorithm loops through blocks and transactions, branches on sender identity, and repeatedly reassigns a running balance. section 4.7
  • A functional implementation makes the address and ledger explicit, filters genesis blocks, flattens transactions, maps each transaction to a balance contribution, and reduces with immutable summation. section 4.7

Delegating from Wallet

A Wallet can keep its public API small while delegating calculation to a side-effect-free function: balance(ledger) returns computeBalance(this.address, ledger).

How The Joy of JavaScript covers it

How deep it goes
Explains loops, branching, side effects, delegation, and a map/filter/reduce implementation, stopping short of broader ledger validation.
Examples
Uses Wallet.balance, an imperative ledger traversal, computeBalance, balanceOf, flat or flatMap, and Money.sum.
What it assumes
Assumes familiarity with wallets, ledgers, transactions, and array methods while demonstrating functional transformation techniques.
What you can do after
Build it from scratch by transforming nested ledger loops into composable, side-effect-free balance calculations.
How it connects
Connects wallets, blockchains, ledgers, transactions, functional programming, object-oriented programming, and array transformations.

This explainer was generated by Manning's knowledge graph from the text of The Joy of JavaScript. The section links point to the parts of the book it draws on.