javascript 拉姆达的东西

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 拉姆达的东西相关的知识,希望对你有一定的参考价值。

var R = require('ramda');

var states = [
    {symbol: 'CT', name: 'Connecticut', pop: 3574097},
    {symbol: 'ME', name: 'Maine', pop: 1328361},
    {symbol: 'MA', name: 'Massachusetts', pop: 6547629},
    {symbol: 'NH', name: 'New Hampshire', pop: 1316470},
    {symbol: 'RI', name: 'Rhode Island', pop: 1052567},
    {symbol: 'VT', name: 'Vermont', pop: 623741},
];

var notSmall = (state) => state.pop >= 2000000;

R.reduce((accum, state) => notSmall(state) ? 
                           R.assoc(state.symbol, state.pop, accum) : 
                           accum,
{}, states);  //=> {"CT": 3574097, "MA": 6547629}

R.pipe(
    R.filter(notSmall), 
    R.map(R.props(['symbol', 'pop'])),
    R.fromPairs)(states) //=> {"CT": 3574097, "MA": 6547629}



R.reduce((accum, state) => notSmall(state) ?  R.assoc(state.symbol, state.pop, accum) : accum, {}, states);

R.pipe(
    R.filter(notSmall),
    R.map(R.props(['symbol', 'pop'])),
    R.fromPairs)(states);

R.fromPairs(R.map(R.props(['symbol', 'pop']), R.filter(notSmall, states)))


R.fromPairs([[ 'CT', 3574097 ]])
R.pipe(R.props(['symbol', 'pop']), R.fromPairs)([ 'CT', 3574097 ]);

R.map(R.pipe(R.props(['symbol', 'pop']), R.fromPairs), R.filter(notSmall, states));

// Path

R.path(['a', 'b'], {a: {b: 2}});

// Combining functions

const isEven = x => x % 2 === 0

R.find(isEven, [1, 2, 3, 4])
R.find(R.complement(isEven), [1, 2, 3, 4])

R.filter(isEven, [1, 2, 3, 4])
R.filter(R.complement(isEven), [1, 2, 3, 4])

// Both/ Either/ Curry

var wasBornInCountry = R.curry((country, person) => person.birthCountry === country)

wasBornInCountry('GB') //=> [Function]
wasBornInCountry('GB', {birthCountry: 'GB'}) //=> true
wasBornInCountry('GB')({birthCountry: 'GB'}) //=> true
wasBornInCountry('GB')({birthCountry: 'IE'}) //=> false

var wasNaturalized = person => Boolean(person.naturalizationDate)
wasNaturalized({birthCountry: 'IE', naturalizationDate: '2014-01-01'}) //=> true
wasNaturalized({}) //=> false

var isOver18 = person => person.age >= 18
isOver18({age: 17}) //=> false
isOver18({age: 18}) //=> true
isOver18({age: 19}) //=> true

var isCitizen = R.curry((country, person) => R.either(wasBornInCountry(country), wasNaturalized)(person))
isCitizen('GB')({birthCountry: 'IE', naturalizationDate: '2014-01-01'})
isCitizen('GB', {birthCountry: 'IE', naturalizationDate: '2014-01-01'})
isCitizen('GB', {birthCountry: 'GB'})

var isEligibleToVote = (country, person) => R.both(isCitizen(country), isOver18)(person)
isEligibleToVote('GB', {birthCountry: 'GB', age: 19}) //=> true
isEligibleToVote('GB', {birthCountry: 'GB', age: 17}) //=> false
isEligibleToVote('GB', {birthCountry: 'IE', age: 19}) //=> false
isEligibleToVote('GB', {birthCountry: 'IE', age: 19, naturalizationDate: '2014-01-01'}) //=> true

var isEligibleToVoteGB = R.curry(isEligibleToVote)('GB');
isEligibleToVote('GB', {birthCountry: 'GB', age: 19}) //=> true
isEligibleToVoteGB({birthCountry: 'IE', age: 19}) //=> false
isEligibleToVoteGB({birthCountry: 'IE', age: 19, naturalizationDate: '2014-01-01'}) //=> true


var gt10 = x => x > 10;
var even = x => x % 2 === 0;
var f = R.either(gt10, even);
f(101); //=> true
f(8); //=> true
f(7) //=> false

// Define a curried greater than function
var gt = R.curry((x, n) => n > x);
gt(10, 11) //=> true
gt(10, 10) //=> false
gt(10, 9) //=> false

// Redefine the greater than 10 function by only supplying the first argument
// to our curried greater than function which will return a new function which
// accepts a single argument (the number to test)
var gt10 = gt(10)
gt10

var f2 = R.either(gt10, even);
f2(101); //=> true
f2(8) //=> true
f2(7) //=> false

var f3 = R.both(gt10, even);
f3(101); //=> false
f3(8) //=> false
f3(7) //=> false
f3(102); //=> true

以上是关于javascript 拉姆达的东西的主要内容,如果未能解决你的问题,请参考以下文章

波的干涉中德塔r=k倍的拉姆达的k怎么去

什么是拉姆达? [复制]

javascript 拉姆达系列

马拉雅拉姆语搜索的自动完成

【科普】拉姆齐定理RamseyTheory-2

JavaScript简介