Given the function below, how do I convert it to point-free style? Would be nice to use Ramda's prop and path and skip the data argument, but I just can't figure out the proper syntax.
const mapToOtherFormat = (data) => (
    {
        'Name': data.Name
        'Email': data.User.Email,
        'Foo': data.Foo[0].Bar
    });
One option would be to make use of R.applySpec, which creates a new function that builds objects by applying the functions at each key of the supplied "spec" against the given arguments of the resulting function.
const mapToOtherFormat = R.applySpec({
  Name: R.prop('Name'),
  Email: R.path(['User', 'Email']),
  Foo: R.path(['Foo', 0, 'Bar'])
})
const result = mapToOtherFormat({
  Name: 'Bob',
  User: { Email: '[email protected]' },
  Foo: [{ Bar: 'moo' }, { Bar: 'baa' }]
})
console.log(result)<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.22.1/ramda.min.js"></script>Here's my attempt:
const mapToOtherFormat = R.converge(
    (...list) => R.pipe(...list)({}),
    [
      R.pipe(R.view(R.lensProp('Name')), R.set(R.lensProp('Name'))),
      R.pipe(R.view(R.compose(R.lensProp('User'), R.lensProp('Email'))), R.set(R.lensProp('Email'))),
      R.pipe(R.view(R.compose(R.lensProp('Foo'), R.lensIndex(0), R.lensProp('Bar'))), R.set(R.lensProp('Foo')))
    ]
  )
const obj = {Name: 'name', User: {Email: 'email'}, Foo: [{Bar: 2}]}
mapToOtherFormat(obj)
Ramda console
[Edit] We can make it completely point-free:
const mapToOtherFormat = R.converge(
    R.pipe(R.pipe, R.flip(R.call)({})),
    [
      R.pipe(R.view(R.lensProp('Name')), R.set(R.lensProp('Name'))),
      R.pipe(R.view(R.compose(R.lensProp('User'), R.lensProp('Email'))), R.set(R.lensProp('Email'))),
      R.pipe(R.view(R.compose(R.lensProp('Foo'), R.lensIndex(0), R.lensProp('Bar'))), R.set(R.lensProp('Foo')))
    ]
  )
Ramda console
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With