chaining.md (1029B)
1 # Chaining 2 3 Math.js supports chaining operations by wrapping a value into a `Chain`. 4 A chain can be created with the function `math.chain(value)` 5 (formerly `math.select(value)`). 6 All functions available in the math namespace can be executed via the chain. 7 The functions will be executed with the chain's value as the first argument, 8 followed by extra arguments provided by the function call itself. 9 10 ```js 11 math.chain(3) 12 .add(4) 13 .subtract(2) 14 .done() // 5 15 16 math.chain( [[1, 2], [3, 4]] ) 17 .subset(math.index(0, 0), 8) 18 .multiply(3) 19 .done() // [[24, 6], [9, 12]] 20 ``` 21 22 ### API 23 24 A `Chain` is constructed as: 25 26 ```js 27 math.chain() 28 math.chain(value) 29 ``` 30 31 The `Chain` has all functions available in the `math` namespace, and has 32 a number of special functions: 33 34 - `done()` 35 Finalize the chain and return the chain's value. 36 - `valueOf()` 37 The same as `done()`, returns the chain's value. 38 - `toString()` 39 Executes `math.format(value)` onto the chain's value, returning 40 a string representation of the value. 41