1 |
|
2 |
|
3 | # Function chain
|
4 |
|
5 | Wrap any value in a chain, allowing to perform chained operations on
|
6 | the value.
|
7 |
|
8 | All methods available in the math.js library can be called upon the chain,
|
9 | and then will be evaluated with the value itself as first argument.
|
10 | The chain can be closed by executing `chain.done()`, which returns
|
11 | the final value.
|
12 |
|
13 | The chain has a number of special functions:
|
14 |
|
15 | - `done()` Finalize the chain and return the chain's value.
|
16 | - `valueOf()` The same as `done()`
|
17 | - `toString()` Executes `math.format()` onto the chain's value, returning
|
18 | a string representation of the value.
|
19 |
|
20 |
|
21 | ## Syntax
|
22 |
|
23 | ```js
|
24 | math.chain(value)
|
25 | ```
|
26 |
|
27 | ### Parameters
|
28 |
|
29 | Parameter | Type | Description
|
30 | --------- | ---- | -----------
|
31 | `value` | * | A value of any type on which to start a chained operation.
|
32 |
|
33 | ### Returns
|
34 |
|
35 | Type | Description
|
36 | ---- | -----------
|
37 | math.Chain | The created chain
|
38 |
|
39 |
|
40 | ## Examples
|
41 |
|
42 | ```js
|
43 | math.chain(3)
|
44 | .add(4)
|
45 | .subtract(2)
|
46 | .done() // 5
|
47 |
|
48 | math.chain( [[1, 2], [3, 4]] )
|
49 | .subset(math.index(0, 0), 8)
|
50 | .multiply(3)
|
51 | .done() // [[24, 6], [9, 12]]
|
52 | ```
|
53 |
|
54 |
|