Last active
August 29, 2015 14:19
-
-
Save safareli/f1128277f312f12e796a to your computer and use it in GitHub Desktop.
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
// some test data | |
var data = [ | |
{value: '0.123', id:'1'}, | |
{value: '0.231', id:'2'}, | |
{value: '0.323', id:'3'}, | |
{value: '0.423', id:'4'}, | |
] | |
// creator of object Property lens | |
, propLens = function(prop){ | |
return R.lens( | |
//geter | |
R.prop(prop), | |
//seter | |
function set(val, obj) { | |
//clone object and set value on that | |
var newObj = R.clone(obj); | |
newObj[prop] = val | |
return newObj; | |
} | |
) | |
} | |
//create lenses of properties we need | |
, idLens = propLens('id') | |
, valueLens = propLens('value') | |
// map over data | |
, algorithm = R.map( | |
R.compose( | |
//map value with parseFloat | |
valueLens.map(parseFloat), | |
//map id with parseFloat | |
idLens.map(parseFloat) | |
) | |
) | |
//parse data | |
, parsedData = algorithm(data); | |
console.log(parsedData,data); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The propLens can be created like this:
var propLens = R.converge(R.lens, R.prop, R.assoc);