+
Skip to content

Implement Haskell lens package functions #118

@DzyubSpirit

Description

@DzyubSpirit

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);

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载