[Functional Programming] Running though a serial number prediction functions for tagging, pairing th...

博客围绕预测函数展开,提及为每个预测函数设置对应标签,编写快速测试函数,将代码重构为无点风格。还介绍了'mreduceMap'函数用于合并数据,使用'flip'和'fanout'等操作,最后给出完整代码,此编码模式可通过替换标签和预测函数复用。

Let's we have some prediction functions, for each prediction function has a corresponding tag:

const {gt, lte, gte, lt} = require('ramda');
const {flip} = require('crocks');

const getSmlPred = and(flip(gt, -1), flip(lte, 50));
const getMedPred = and(flip(gt, 50), flip(lte, 100));
const getLrgPred =  flip(gt, 100);
// taggedPred :: (b, (a -> Boolean))
// taggedPreds :: [taggedPred]
const taggedPreds = [
    ['Sml', getSmlPred],
    ['Med', getMedPred],
    ['Lrg', getLrgPred]
];

So if we have input as:

15 -> 'Sml';
60 -> 'Med'
101 -> 'Lrg'

Also we wish our program to the safe, we want:

-1 -> Nothing

 

We can write a quick test function:

const {gt, lte, gte, lt} = require('ramda');

const {First, mreduceMap, and, safe, option, flip, objOf, assoc, not,
    merge, identity, fanout, curry, compose, map, constant} = require('crocks');


const getSmlPred = and(flip(gt, -1), flip(lte, 50));
const getMedPred = and(flip(gt, 50), flip(lte, 100));
const getLrgred =  flip(gt, 50);
// taggedPred :: (b, (a -> Boolean))
// taggedPreds :: [taggedPred]
const taggedPreds = [
    ['Sml', getSmlPred],
    ['Med', getMedPred],
    ['Lrg', getLrgred]
];

const fn = curry(([tag, pred]) => x => safe(pred)(x)
    .map(constant(tag)));

console.log('40 - Sml', fn(taggedPreds[0], 40)); // Just "Sml"
console.log('60 - Med', fn(taggedPreds[1], 60)); // Just "Med"
console.log('101 - Lrg', fn(taggedPreds[2], 101)); // Just "Lrg"

  

We can refactor the code into a more pointfree style and rename the testing 'fn' to 'tagValue':

const {gt, lte, gte, lt} = require('ramda');

const {First, mreduceMap, and, safe, option, flip, objOf, assoc, not,
    merge, identity, fanout, curry, compose, map, constant} = require('crocks');


const getSmlPred = and(flip(gt, -1), flip(lte, 50));
const getMedPred = and(flip(gt, 50), flip(lte, 100));
const getLrgred =  flip(gt, 50);
// taggedPred :: (b, (a -> Boolean))
// taggedPreds :: [taggedPred]
const taggedPreds = [
    ['Sml', getSmlPred],
    ['Med', getMedPred],
    ['Lrg', getLrgred]
];
// tagValue :: taggedPred -> a -> Maybe b
const tagValue = curry(([tag, pred]) => compose(
    map(constant(tag)),
    safe(pred)
));    

console.log('40 - Sml', tagValue(taggedPreds[0], 40)); // Just "Sml"
console.log('60 - Med', tagValue(taggedPreds[1], 60)); // Just "Med"
console.log('101 - Lrg', tagValue(taggedPreds[2], 101)); // Just "Lrg"
console.log('-1 - Nothing', tagValue(taggedPreds[0], -5)); // Nothing "Nothing"

 

Now, what we want is create fews functions, we know that we want data comes last, we want to partiaclly apply the predcitions functions.

// match :: [ taggedPreds ] -> a -> Maybe b
const match = 
    flip(x => mreduceMap(First, flip(tagValue, x)));  
const matchNumber = match(taggedPreds);
console.log('matchNumber', matchNumber(49)); // Just "Sml"

'mreduceMap': it take Monoid for combine the data together, here we use 'First', so only take the first Maybe result. 

Then for each [tag, pred], we will run with tagValue([tag, pred], x), because [tag, pred] will be partiaclly applied last, but 'tagValue' function takes it as first arguement, so we have to use 'flip'. And apply 'x' first.

In the test code above, we get ''Just Sml" back for number 49. It is good, but not enough, what we really want is keeping a Pair(a, s), for example: Pair('Sml', 40); we can keep the original state together with the tag result. 

 

What we can do is using 'fanout', which take two functions and generate a Pair:

const tagCard = fanout(compose(option(' | '), matchNumber), objOf('number'));
console.log('tagCard', tagCard(49)); // Pair( "Sml", { number: 49 } )

 

Lastly, we want to have the final result as:

const cardFromNumber = compose(
    merge(assoc('type')),
    tagCard
);

console.log(
    cardFromNumber(101)
) // { number: 101, type: 'Lrg' }

 

Full Code:

---

const {gt, lte} = require('ramda');

const {First, mreduceMap, and, safe, option, flip, objOf, assoc,
    merge, fanout, curry, compose, map, constant} = require('crocks');


const getSmlPred = and(flip(gt, -1), flip(lte, 50));
const getMedPred = and(flip(gt, 50), flip(lte, 100));
const getLrgred =  flip(gt, 100);
// taggedPred :: (b, (a -> Boolean))
// taggedPreds :: [taggedPred]
const taggedPreds = [
    ['Sml', getSmlPred],
    ['Med', getMedPred],
    ['Lrg', getLrgred]
];

// tagValue :: taggedPred -> a -> Maybe b
const tagValue = curry(([tag, pred]) => compose(
    map(constant(tag)),
    safe(pred)
));    

// match :: [ taggedPreds ] -> a -> Maybe b
const match = 
    flip(x => mreduceMap(First, flip(tagValue, x)));  
const matchNumber = match(taggedPreds);

const tagCard = fanout(compose(option(' | '), matchNumber), objOf('number'));

const cardFromNumber = compose(
    merge(assoc('type')),
    tagCard
)

console.log(
    cardFromNumber(101)
) // { number: 101, type: 'Lrg' }

 

---

This coding partten is useful, because we can keep the structure, just replace the tags and predications functions:

// data.js
const {test} = require('ramda');

module.exports = [
    ['Diners - Carte Blanche|diners', test(/^30[0-5]/)],
    ['Diners|diners', test(/^(30[6-9]|36|38)/)],
    ['JCB|jcb', test(/^32(2[89]|[3-8][0-9])/)],
    ['AMEX|american-express', test(/^3[47]/)],
    ['Visa Electron|visa', test(/^(4026|417500|4508|4844|491(3|7))/)]
]
const {gt, lte} = require('ramda');

const {First, mreduceMap, and, safe, option, flip, objOf, assoc,
    merge, fanout, curry, compose, map, constant} = require('crocks');


const getSmlPred = and(flip(gt, -1), flip(lte, 50));
const getMedPred = and(flip(gt, 50), flip(lte, 100));
const getLrgred =  flip(gt, 100);
// taggedPred :: (b, (a -> Boolean))
// taggedPreds :: [taggedPred]
const taggedPreds = require('./data.js');
// tagValue :: taggedPred -> a -> Maybe b
const tagValue = curry(([tag, pred]) => compose(
    map(constant(tag)),
    safe(pred)
));    

// match :: [ taggedPreds ] -> a -> Maybe b
const match = 
    flip(x => mreduceMap(First, flip(tagValue, x)));  
const matchNumber = match(taggedPreds);

const tagCard = fanout(compose(option(' | '), matchNumber), objOf('number'));

const cardFromNumber = compose(
    merge(assoc('type')),
    tagCard
)

console.log(
    cardFromNumber('4026-xxxx-xxxxx-xxxx')
) // { number: '4026-xxxx-xxxxx-xxxx', type: 'Visa Electron|visa' }

 

转载于:https://www.cnblogs.com/Answer1215/p/10561400.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值