Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bd6c54d

Browse files
authored
Pipelines
1 parent 39beea6 commit bd6c54d

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

‎README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,5 +215,26 @@ let DrinkTotal = cart.filter(x => x.name === "Drink")
215215
.map(x => x.price)
216216
.reduce((t, v) => t += v)
217217
.toFixed(2);
218+
218219
console.log(Total Drink Cost $${drinkTotal}); // Total Drink Cost 14ドル.13
219220
```
221+
222+
## Pipelines
223+
224+
A pipeline allows for easy function composition when performing multiple operations on a variable. Since JavaScript lacks a Pipeline operator, a design pattern can be used to accomplish the task.
225+
226+
```javascript
227+
const pipe = functions => data => {
228+
return functions.reduce(
229+
(value, func) => func(value),
230+
data
231+
);
232+
};
233+
let cart = [3.12, 45.15, 11.01];
234+
const addSalesTax = (total, taxRate) => (total * taxRate) + total;
235+
const tally = orders => pipe([
236+
x => x.reduce((total, val) => total + val), // sum the order
237+
x => addSalesTax(x, 0.09),
238+
x => `Order Total = ${x.toFixed(2)}` // convert to text
239+
])(orders); // Order Total = 64.62
240+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /