简体   繁体   English

如何将多个参数传递给R.cond ramda function?

[英]How to pass multiple parameters to R.cond ramda function?

Here is the ramda function that accept a string parameter这是接受字符串参数的 ramda function

const decodeBarcode => R.cond([
  [x => R.equals(x.length, 12), decodeUPACBarcode],
  [x => R.equals(x.length, 13), decode13DigitBarcode],
  [x => R.gt(x.length, 20), decodeLengthGT20Barcode],
  [R.T, R.identity],
]);

How to pass another parameter to improve condition?如何传递另一个参数来改善条件?

another parameter barcodeType to be included要包含的另一个参数 barcodeType

if (barcode.length > 20 or barcodeType = "DATAMATRIX") 

Tried the below, not working !!尝试了以下,不工作!

 const decodeBarcode = (barcode, barcodeType) => R.cond([
      [x => R.equals(x.length, 12), decodeUPACBarcode],
      [x => R.equals(x.length, 13), decode13DigitBarcode],
      [x => R.or(R.propEq('DATAMATRIX', barcodeType), R.gt(x.length, 20)), decodeLengthGT20Barcode],
      [R.T, R.identity],
    ])(barcode);

RamdaJS REPL RamdaJS REPL

You can use R.anyPass to run a list of predicates against a value, and return true is any of the predicate returns true :您可以使用R.anyPass针对一个值运行谓词列表,并且返回true是任何谓词返回true

 const { cond, propEq, anyPass, propSatisfies, lte, T, identity } = R const decodeLength13Barcode = () => console.log('Its 13 digit barcode') const decodeLengthGT20Barcode = () => console.log('Its > 20 digit barcode') const decodeBarcode = (barcode, barcodeType) => cond([ [propEq('length', 13), decodeLength13Barcode], [anyPass([ propEq('DATAMATRIX', barcodeType), propSatisfies(lte(20), 'length') ]), decodeLengthGT20Barcode], [T, identity], ])(barcode); console.log(decodeBarcode('0272889010009')); console.log(decodeBarcode('02728890100099898989')); console.log(decodeBarcode({ DATAMATRIX: '20d' }, '20d'));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.28.0/ramda.min.js" integrity="sha512-t0vPcE8ynwIFovsylwUuLPIbdhDj6fav2prN9fEu/VYBupsmrmk9x43Hvnt+Mgn2h5YPSJOk7PMo9zIeGedD1A==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

Since R.cond passes all parameters to the predicates, you can remove the arrow function (point-free).由于R.cond将所有参数传递给谓词,您可以删除箭头 function(无点)。 However, you pass bardcode, barcodeType , while R. propEq expects barcodeType, barcode , so you'll need to flip it after stating the property ( DATAMATRIX ):但是,您传递bardcode, barcodeType ,而R. propEq需要barcodeType, barcode ,因此您需要在声明属性 ( DATAMATRIX ) 后翻转它:

 const { cond, propEq, anyPass, flip, propSatisfies, lte, T, identity } = R const decodeLength13Barcode = () => console.log('Its 13 digit barcode') const decodeLengthGT20Barcode = () => console.log('Its > 20 digit barcode') const decodeBarcode = cond([ [propEq('length', 13), decodeLength13Barcode], [anyPass([ flip(propEq('DATAMATRIX')), propSatisfies(lte(20), 'length') ]), decodeLengthGT20Barcode], [T, identity], ]); console.log(decodeBarcode('0272889010009')); console.log(decodeBarcode('02728890100099898989')); console.log(decodeBarcode({ DATAMATRIX: '20d' }, '20d'));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.28.0/ramda.min.js" integrity="sha512-t0vPcE8ynwIFovsylwUuLPIbdhDj6fav2prN9fEu/VYBupsmrmk9x43Hvnt+Mgn2h5YPSJOk7PMo9zIeGedD1A==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

If I understand correctly, you want something like this:如果我理解正确的话,你想要这样的东西:

 const decodeBarcode = cond ([ [propEq ('length', 12), decodeUPACBarcode], [propEq ('length', 13), decode13DigitBarcode], [(barcode, barcodeType) => barcode.length > 20 || barcodeType == "DATAMATRIX", decodeLengthGT20Barcode], [T, identity], ]); const testCases = [ ['123456789012', 'TYPEA'], ['1234567890123'], ['123456789012345678901'], ['12345678901234567', 'DATAMATRIX'], ['12345678901324567'] ] testCases.forEach (xs => console.log ( `decodeBarcode ('${xs.join(`', '`)}')\n//==> ${decodeBarcode (...xs)}` ))
 .as-console-wrapper {max-height: 100%;important: top: 0}
 <script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.28.0/ramda.min.js"></script> <script> const {cond, propEq, T, identity} = R const decodeUPACBarcode = (barcode, type = '') => `calling decodeUPACBarcode('${barcode}', '${type}')` const decode13DigitBarcode = (barcode, type = '') => `calling decode13DigitBarcode('${barcode}', '${type}')` const decodeLengthGT20Barcode = (barcode, type = '') => `calling decodeLengthGT20Barcode('${barcode}', '${type}')` </script>

While we always have a way to make a function point-free, and it was worth doing for the first two conditions, the third one is going to be ugly enough that I wouldn't do it except as academic exercise.虽然我们总是有办法使 function 无积分,并且在前两个条件下值得这样做,但第三个条件会很丑陋,除非作为学术练习,否则我不会这样做。 It might end up being something like this:它可能最终是这样的:

pipe (
  unapply (zipWith (call) ([propSatisfies (gt (__, 20), 'length'), equals ('DATAMATRIX')])),
  apply (or)
)

and that is much less readable than the arrow function currently used.这比当前使用的箭头 function 可读性差得多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM