-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpointfree2.js
60 lines (46 loc) · 1.55 KB
/
pointfree2.js
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
const R = require('ramda')
const profile = {
id: '1234',
name: 'Amy'
}
const genStudentNo = (id) => `00{id}`
const updateProfile = (profile) => {
const studentNo = genStudentNo(profile.id)
return R.assoc('number', studentNo, profile)
}
const result = updateProfile(profile)
console.log(result)
// s1
const updateProfile = (profile) => {
const studentNo = genStudentNo(R.propOr('0000', 'id')(profile))
return R.assoc('number', studentNo, profile)
}
// s2
const updateProfile = (profile) => {
const studentNo = R.compose(genStudentNo, R.propOr('0000', 'id'))(profile)
return R.assoc('number', studentNo, profile)
}
// s3
const getStudentNoFromProfile = R.compose(genStudentNo, R.propOr('0000', 'id'))
const updateProfile = (profile) => {
const studentNo = getStudentNoFromProfile(profile)
return R.assoc('number', studentNo, profile)
}
// s4
const getStudentNoFromProfile = R.compose(genStudentNo, R.propOr('0000', 'id'))
const updateProfile = (profile) => R.assoc('number', getStudentNoFromProfile(profile), profile)
// s5
const getStudentNoFromProfile = R.compose(genStudentNo, R.propOr('0000', 'id'))
const updateProfile = R.converge(R.assoc('number'), [getStudentNoFromProfile, R.identity])
// -------------------------
// R.assoc
R.assoc('c', 3, {a: 1, b: 2}) //=> {a: 1, b: 2, c: 3}
// R.propOr
const alice = {
name: 'ALICE',
age: 101
}
const favorite = R.prop('favoriteLibrary')
const favoriteWithDefault = R.propOr('Ramda', 'favoriteLibrary')
favorite(alice) //=> undefined
favoriteWithDefault(alice) //=> 'Ramda'