[Compose] Compose exercises

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Setup
//==============
const _ = R;
const {formatMoney} = accounting;
 
// Example Data
const CARS = [
    {name: "Ferrari FF", horsepower: 660, dollar_value: 700000, in_stock: true},
    {name: "Spyker C12 Zagato", horsepower: 650, dollar_value: 648000, in_stock: false},
    {name: "Jaguar XKR-S", horsepower: 550, dollar_value: 132000, in_stock: false},
    {name: "Audi R8", horsepower: 525, dollar_value: 114200, in_stock: false},
    {name: "Aston Martin One-77", horsepower: 750, dollar_value: 1850000, in_stock: true},
    {name: "Pagani Huayra", horsepower: 700, dollar_value: 1300000, in_stock: false}
  ];
 
// Exercise 1:
// ============
// use _.compose() to rewrite the function below. Hint: _.prop() is curried.
 
const isLastInStock = _.compose(
  _.prop('in_stock'),
  _.last
)
 
QUnit.test("Ex1: isLastInStock", assert => {
  assert.deepEqual(isLastInStock(CARS), false);
})
 
''
 
 
// Exercise 2:
// ============
// use _.compose(), _.prop() and _.head() to retrieve the name of the first car
 
const nameOfFirstCar = _.compose(
  _.prop('name'),
  _.head
)
 
QUnit.test("Ex2: nameOfFirstCar", assert => {
  assert.equal(nameOfFirstCar(CARS), "Ferrari FF");
})
 
// Exercise 3:
// ============
// Use the helper function _average to refactor averageDollarValue as a composition
 
const _average = function(xs) { return _.reduce(_.add, 0, xs) / xs.length; }; // <- leave be
 
const averageDollarValue = _.compose(
  _average,
  _.map(_.prop('dollar_value'))
)
 
QUnit.test("Ex3: averageDollarValue", assert => {
  assert.equal(averageDollarValue(CARS), 790700);
})
 
 
// Exercise 4:
// ============
// Write a function: sanitizeNames() using compose that returns a list of lowercase and underscored names: e.g: sanitizeNames(["Hello World"]) //=> ["hello_world"].
 
const _underscore = _.replace(/\W+/g, '_'); //<-- leave this alone and use to sanitize
 
const sanitizeNames = _.map(
  _.compose(
    _underscore,
    _.toLower,
    _.prop('name')
  )
)
 
QUnit.test("Ex4: sanitizeNames", assert => {
  assert.deepEqual(sanitizeNames(CARS), ['ferrari_ff', 'spyker_c12_zagato', 'jaguar_xkr_s', 'audi_r8', 'aston_martin_one_77', 'pagani_huayra']);
})
 
 
 
// Bonus 1:
// ============
// Refactor availablePrices with compose.
const formatDollarValue = _.compose(
      formatMoney,
      _.prop('dollar_value')
    )
const availablePrices = _.compose(
  _.join(', '),
  _.map(formatDollarValue),
  _.filter(_.prop('in_stock'))
)
 
QUnit.test("Bonus 1: availablePrices", assert => {
  assert.deepEqual(availablePrices(CARS), '$700,000.00, $1,850,000.00');
})
 
// Bonus 2:
// ============
// Refactor to pointfree.
const append = _.flip(_.concat)
const fastestCar = _.compose(
  append(' is the fastest'),
  _.prop('name'),
  _.last,
  _.sortBy(_.prop('horsepower'))
)
 
QUnit.test("Bonus 2: fastestCar", assert => {
  assert.equal(fastestCar(CARS), 'Aston Martin One-77 is the fastest');
})

  

posted @   Zhentiw  阅读(187)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2019-04-26 [Docker] Running Multiple Containers for an Angular, Node project
2017-04-26 [Django] Auth django app with rest api
2017-04-26 [Angular] Pluck value from Observable
2016-04-26 [Angular 2] Using ngrx/store and Reducers for Angular 2 Application State
2016-04-26 [Angular 2] Mapping Streams to Values to Affect State
2016-04-26 [Angular 2] Managing State in RxJS with StartWith and Scan
2016-04-26 [Angular 2] Handling Clicks and Intervals Together with Merge
点击右上角即可分享
微信分享提示