-
-
Save azl397985856/f21130f28886aada1e2d7e892e105ace to your computer and use it in GitHub Desktop.
monad-code-pen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="content">123</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// // ex:1 | |
// var safeProp = R.curry(function (x, o) { return R.of(o[x]); }); | |
// var user = [{ | |
// id: 334, | |
// name: "albert", | |
// address: { | |
// stre11et: { | |
// number: 22, | |
// name: 'hehe' | |
// } | |
// } | |
// },{ | |
// id: 2, | |
// name: "albert", | |
// address: { | |
// street: { | |
// number: 22, | |
// name: 'Walnut St' | |
// } | |
// } | |
// }]; | |
// // console.log(user.address.street.name); | |
// // safeProp('name')safeProp('street')safeProp('address') | |
// var getAddress = safeProp('address'); | |
// var getStreet = safeProp('street'); | |
// var getName = safeProp('name'); | |
// var ret = R.map(R.compose(R.chain(getName), R.chain(getStreet),getAddress))(user); | |
// console.log(ret); | |
// var Maybe = function(x) { | |
// this.__value = x; | |
// } | |
// Maybe.of = function(x) { | |
// return new Maybe(x); | |
// } | |
// Maybe.prototype.isNothing = function() { | |
// return (this.__value === null || this.__value === undefined); | |
// } | |
// Maybe.prototype.map = function(f) { | |
// return this.isNothing() ? Maybe.of(null) : Maybe.of(f(this.__value)); | |
// } | |
// const a = Maybe.of({a: 1}).map((v)=>(v.c)).map((v)=>{v.b}).map((v)=>{v.ccc}); | |
// // const a = Maybe.of({a: 1}).map(R.prop('b')).map(R.prop('dd')); | |
// // console.log(a); | |
// var duplicate = n => [n, n]; | |
// const ret = R.chain(duplicate, [1, 2, 3]); //=> [1, 1, 2, 2, 3, 3] | |
// console.log(ret); | |
// truncate :: String -> String | |
var truncate = R.when( | |
R.propSatisfies(R.gt(R.__, 10), 'length'), | |
R.pipe(R.take(10), R.append('…'), R.join('')) | |
); | |
const getArea = function (payload) { | |
return R.when(!R.isNil(payload.country.province.area), payload.country.province.area); | |
} | |
const area = getArea({ | |
country: | |
{ | |
province: { | |
area: '2121' | |
} | |
} | |
}); | |
console.log(area); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="http://npmcdn.com/ramda/dist/ramda.js"></script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment