【问题标题】:ramda js concat the value using reduceramda js使用reduce连接值
【发布时间】:2019-02-12 17:12:50
【问题描述】:

我有一个将转换为字符串的数据数组。我已经创建了进行转换的函数。我的问题是编写此函数以使其更具可读性的最佳方法是什么?我不想有多个if/else 语句。

const data = [
    "hello",
    {name: "Bob"},
    "and",
    {name: "Fred"},
    "How are you guys today?",
]

const isString = R.is(String)
const isObject = R.is(Object)

const getName = R.prop('name')

const toPureString = R.reduce(
    (result, value) => {
        if (isString(value)) {
            return `${result}${value} `
        }
        if (isObject(value)) {
            return `${result}${getName(value)}`
        } 
        return result
    }, "")

toPureString(data)
// hello Bob and Fred How are you guys today?

【问题讨论】:

    标签: javascript functional-programming ramda.js


    【解决方案1】:

    我会做这样的事情:

    const {compose, join, map, unless, is, prop} = R
    const data = ['hello', {name: 'Bob'}, 'and', {name: 'Fred'}, 'How are you guys today?']
    
    const transform = compose(join(' '), map(unless(is(String), prop('name'))))
    
    console.log(transform(data))
    <script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>

    unless 是一个简化的if-else,它返回原始值,除非谓词返回 true,在这种情况下它首先应用转换函数。 (when 类似,除了它应用转换谓词为真。)所以这分两步进行,首先将所有内容转换为一致的格式,纯字符串,然后将它们组合起来。

    这对您来说可能更好读,但它们是完全等价的:

    const transform = pipe(
      map(unless(is(String), prop('name'))),
      join(' '), 
    )
    

    【讨论】:

      【解决方案2】:

      你可以使用R.cond:

      const { flip, is, unapply, join, useWith, prop, T, identity } = R
      
      const isString = flip(is(String))
      const isObject = flip(is(Object))
      const spacer = unapply(join(' '))
      const getName = useWith(spacer, [identity, prop('name')])
      
      const toPureString = R.reduce(R.cond([
        [isString, spacer],
        [isObject, getName],
        [T, identity]
      ]), '')
      
      const data = [
          "hello",
          {name: "Bob"},
          "and",
          {name: "Fred"},
          "How are you guys today?",
      ]
      
      const result = toPureString(data)
      
      console.log(result) // hello Bob and Fred How are you guys today?
      <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>

      【讨论】:

        【解决方案3】:

        Ramda 有一个ifElse 函数。但是,当您的 if 语句有多个分支时,它会变得很笨拙:

        const ifStr = R.ifElse(isString, v => `{result}{v}`, () => result);
        return R.ifElse(isObject, v => `{result}{getName(v)}`, ifStr);
        

        对于这种情况,我会使用纯 JavaScript 中的三元语句来实现:

        const toPureString = R.reduce((result, value) => {
            const str = isObject(value) ? getName(value) : (isString(value) ? value : '')
            return `${result}${str}`
        }, '')
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-08-10
          • 2017-09-12
          • 2020-10-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多