Created
January 14, 2016 11:53
-
-
Save stoeffel/1383791888ea071f73bb to your computer and use it in GitHub Desktop.
save lenses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const log = R.map(R.compose(console::console.log, R.toString)); | |
const saveProp = x => R.pipe(R.prop(x), S.toMaybe) | |
var xSave = R.lens(saveProp('x'), R.assoc('x')); | |
var xLens = R.lens(R.prop('x'), R.assoc('x')); | |
log([ | |
R.view(xLens, {x: 1, y: 2}) | |
, R.view(xLens, {noX: 1, y: 3}) | |
, R.view(xSave, {noX: 1, y: 3}) | |
, R.set(xLens, 4, {x: 1, y: 2}) | |
, R.set(xSave, 4, {x: 1, y: 2}) | |
, R.over(xLens, R.negate, {noX: 1, y: 2}) | |
, R.over(xSave, R.map(R.negate), {noX: 1, y: 2}) | |
, R.over(xLens, R.negate, {x: 1, y: 2}) | |
, R.over(xSave, R.map(R.negate), {x: 1, y: 2}) | |
]); | |
const savePath = x => R.pipe(R.path(x), S.toMaybe) | |
var ySave = R.lens(savePath(['x', 'y']), R.assoc('y')); | |
var yLens = R.lens(R.path(['x', 'y']), R.assoc('y')); | |
log([ | |
R.view(yLens, {x: { y: 2 }}) | |
, R.view(yLens, {nox: { y: 2 }}) | |
, R.view(ySave, {nox: { y: 24 }}) | |
, R.set(yLens, 4, {x: { y: 2 }}) | |
, R.set(ySave, 4, {x: { y: 2 }}) | |
, R.over(yLens, R.negate, {nox: { y: 2 }}) | |
, R.over(ySave, R.map(R.negate), {nox: { y: 2 }}) | |
, R.over(yLens, R.negate, {x: { y: 2 }}) | |
, R.over(ySave, R.map(R.negate), {x: { y: 2 }}) | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment