Skip to content

Instantly share code, notes, and snippets.

@safareli
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save safareli/f1128277f312f12e796a to your computer and use it in GitHub Desktop.
Save safareli/f1128277f312f12e796a to your computer and use it in GitHub Desktop.
// 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);
@TheLudd
Copy link

TheLudd commented Apr 17, 2015

The propLens can be created like this:
var propLens = R.converge(R.lens, R.prop, R.assoc);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment