Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Haskell lens package functions #118

Open
DzyubSpirit opened this issue Feb 10, 2018 · 0 comments
Open

Implement Haskell lens package functions #118

DzyubSpirit opened this issue Feb 10, 2018 · 0 comments

Comments

@DzyubSpirit
Copy link
Member

DzyubSpirit commented Feb 10, 2018

Working with GlobalStorage means working with nested objects with deeply nested structures. Haskell solution for that is lenses.
There is rambda-lens implementation for javascript. It's fully functional and so performance is only okay using with immutable-js.
But we can rewrite functions using functionality and mutability for best results.

There are many functions in the lens so I give only a few examples in this issue. There is also an article about using rambda-lens.

Data:

const users = [{
  id: 3,
  name: 'Charles Bronson',
  addresses: [{
    street: '99 Walnut Dr.',
    zip: '04821',
  }, {
    street: '2321 Crane Way',
    zip: '08082',
  }],
}, {
  id: 5,
  name: 'Mr. Ambassador',
  addresses: [{
    street: '2050 Bamako Place',
    zip: '20521-2050',
  }, {
    street: '7100 Athens Place',
    zip: '20521-7100',
  }],
}];

Standard way:

users.forEach((_, i, arr) =>
  arr[i]['addresses'].forEach((_, i, arr) =>
    arr[i]['street'] = '*****'
  )
);

Using lens:

const { set, lensProp, mapped } = require('metarhia-common');
set([mapped, lensProp('addresses'), mapped, lensProp('street')], '*****', users);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants