-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Adds lensIndex, lensProp, composeL & pipeL #971
Conversation
*/ | ||
module.exports = function lensIndex(n) { | ||
return lens(nth(n), function(x, xs) { | ||
return _concat( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably faster to do slice(xs, 0, n].concat([x], _slice(xs, n + 1))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As suggested, I've updated this to use _slice(xs, 0, n).concat([x], _slice(xs, n + 1))
instead.
bd04762
to
133e720
Compare
*/ | ||
module.exports = function(k) { | ||
return lens(prop(k), assoc(k)); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no doubt, this is a super useful pattern
🌿 Very nice stuff! I really like I wonder if we also want |
*/ | ||
module.exports = function lensIndex(n) { | ||
return lens(nth(n), function(x, xs) { | ||
return _slice(xs, 0, n).concat([x], _slice(xs, n + 1)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it worth pulling this out to a separate function, equivalent to assoc
for arrays?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
discussion about whether assoc
should work on arrays as well: #773
This change introduces 2 new helpers for generating lenses, `lensIndex` and `lensProp`, where `lensIndex` is used to create a new lens with a focus on an array index, while `lensProp` is used to create a new lens with a focus on an object property. ```js var phraseLens = R.lensProp('phrase'); var obj1 = { phrase: 'Absolute filth . . . and I LOVED it!'}; phraseLens(obj1); // => 'Absolute filth . . . and I LOVED it!' phraseLens.set('Ooh Betty', obj1); //=> { phrase: 'Ooh Betty'} var headLens = R.lensIndex(0); headLens([10, 20, 30, 40]); //=> 10 headLens.set('mu', [10, 20, 30, 40]); //=> ['mu', 20, 30, 40] ``` This change also introduces `composeL` and `pipeL` which are used to compose lenses together, resulting in a new lens which can be used to access and update nested properties of a target object. ```js var headLens = R.lensIndex(0); var secondLens = R.lensIndex(1); var xLens = R.lensProp('x'); var secondOfXOfHeadLens = R.composeL(secondLens, xLens, headLens); var source = [{x: [0, 1], y: [2, 3]}, {x: [4, 5], y: [6, 7]}]; secondOfXOfHeadLens(source); //=> 1 secondOfXOfHeadLens.set(123, source); //=> [{x: [0, 123], y: [2, 3]}, {x: [4, 5], y: [6, 7]}] ```
133e720
to
e1e9781
Compare
🐄 this looks good. any input @davidchambers ? |
module.exports = function(k) { | ||
return lens(prop(k), assoc(k)); | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👾
I haven't yet used lenses, but these functions make sense to me. |
Adds lensIndex, lensProp, composeL & pipeL
Love this. Was actually just looking for something like |
Looking forward to this! Hopefully next release is soon 👍 |
Should lensIndex this use R.update for the setter? Would this be too much dog food: |
It would not necessarily be too much dogfood. But I would want to compare that to the existing implementation, especially as |
The reason why Using function lensProp(k) {
return lens(prop(k), assoc(k));
} function lensIndex(n) {
return lens(nth(n), update(n));
} |
I would definitely be in favor of that change. |
This change introduces 2 new helpers for generating lenses,
lensIndex
andlensProp
, wherelensIndex
is used tocreate a new lens with a focus on an array index, while
lensProp
is used to create a new lens with a focus on anobject property.
This change also introduces
composeL
andpipeL
whichare used to compose lenses together, resulting in a new lens
which can be used to access and update nested properties
of a target object.