Home > Blockchain >  Ramda - reduce parentheses nesting on composition
Ramda - reduce parentheses nesting on composition

Time:12-26

I have an array like this:

const arr = [
  ['1 1 1', '', '2 2 2'], 
  ['', '3 3 3', '4 4 4']
]

and my goal is to convert it to this array:

[
  [ [1,1,1], [2,2,2] ],
  [ [3,3,3], [4,4,4] ]
]

I am trying to do that in functional way using function composition. I am also using Ramda.

I have this code

const filterEmpty = filter(o(not, isEmpty));

const getFinalArr = map(
  compose( map(map(parseInt)), map(split(' ')), filterEmpty )
)

console.log(getFinalArr(arr))

Is there way to write it with less map nesting? I tried something like this:

const getFinalArr = map(
  compose( parseInt, map, map, split(' '), map, filterEmpty )
)

But of course it did not work.

Or if there is another way to easily deal with arrays nested like this, I would appreciate learning about that.

CodePudding user response:

When things start to get long and confusing I prefer R.pipe on R.compose, and writing the function where each line represents and transformation:

const { map, pipe, reject, isEmpty, split } = R

const fn = map(pipe(
  reject(isEmpty), // remove empty items
  map(split(' ')), // convert string to sub-arrays
  map(map(Number)), // convert sub-arrays to arrays of numbers  
))

const arr = [['1 1 1', '', '2 2 2'], ['', '3 3 3', '4 4 4']]

const result = fn(arr)

console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.min.js" integrity="sha512-rZHvUXcc1zWKsxm7rJ8lVQuIr1oOmm7cShlvpV0gWf0RvbcJN6x96al/Rp2L2BI4a4ZkT2/YfVe/8YvB2UHzQw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

CodePudding user response:

You can do it with pure javascript:

const arr = [
  ['1 1 1', '', '2 2 2'], 
  ['', '3 3 3', '4 4 4']
]

function foo(arr){
  const cleanArray = arr.flat().filter(Boolean)
  const numberArrays = cleanArray.map(suite=> {
    return suite.split(" ").map(n=>  n)
  })
  let batchMemory = []
  return numberArrays.reduce((acc,cur, i)=> {
    const index = i 1
    batchMemory.push(cur)
      if(index %2===0){
        acc.push(batchMemory)
        batchMemory = []
        return acc
      }
    return acc
  }, [])
}

console.log(foo(arr))

CodePudding user response:

You could use a slightly different approach by having a look to a solution in pure Javascritp and convert this to Ramda.

parse

const
    { compose, filter, isEmpty, map, not, o, split } = R,
    array = [['1 1 1', '', '2 2 2'], ['', '3 3 3', '4 4 4']],
    resultPure = array.map(a => a
        .filter(Boolean)
        .map(s => s
            .split(' ')
            .map(Number)
        )
    ),
    fn = map(compose(
        map(compose(
            map(Number),
            split(' ')
        )),
        filter(o(not, isEmpty))
    ));
    
console.log(fn(array));
console.log(resultPure);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.min.js" integrity="sha512-rZHvUXcc1zWKsxm7rJ8lVQuIr1oOmm7cShlvpV0gWf0RvbcJN6x96al/Rp2L2BI4a4ZkT2/YfVe/8YvB2UHzQw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

  • Related