Skip to content

Commit

Permalink
feat(mapper): add convenience mapper method
Browse files Browse the repository at this point in the history
  • Loading branch information
javiercejudo committed Feb 13, 2016
1 parent 7d801c2 commit 3f2818c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,22 @@ fn.raw(d, 10, [2, 3/10]); // d(30)
## Use case: functional hex to dec

```js
var R = require('ramda');
var fn = require('positional-notation');
const R = require('ramda');
const fn = require('positional-notation');

var symbols = {
const symbols = {
'0': '0', '1': '1', '2': '2', '3': '3', '4': '4', '5': '5', '6': '6', '7': '7', '8': '8', '9': '9',
'A': '10', 'B': '11', 'C': '12', 'D': '13', 'E': '14', 'F': '15',
};

var posNotation = fn(Object.keys(symbols).length);
const posNotation = fn(Object.keys(symbols).length);

var hexToDec = R.pipe(
const hexToDec = R.pipe(
R.toUpper,
R.split(''),
R.reverse,
R.map(R.prop(R.__, symbols)),
R.addIndex(R.map)(function(val, index) {
return posNotation([index, val]);
}),
R.addIndex(R.map)(fn.mapper(posNotation)),
R.sum
);

Expand Down
8 changes: 7 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@ var floatingAdapter = require('floating-adapter');

var Decimal = pipe(times, pow)(coreArbitraryPrecision(floatingAdapter));
var toDecimal = toDecimalFactory(Decimal);
var curry3 = curryN(3);

var positionalNotationRaw = curryN(3, function (d, base, posValPair) {
var positionalNotationRaw = curry3(function (d, base, posValPair) {
return d(base).pow(d(posValPair[0]))
.times(d(posValPair[1]));
});

var positionalNotation = curryN(2, pipe(positionalNotationRaw(toDecimal), Number));

positionalNotation.raw = positionalNotationRaw;

positionalNotation.mapper = curry3(function(f, val, index) {
return f([index, val]);
});

module.exports = positionalNotation;
18 changes: 14 additions & 4 deletions test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('positional notation', function() {
});
});

describe('use case', function() {
describe('use case: functional hex to dec', function() {
var symbols = {
'0': '0', '1': '1', '2': '2', '3': '3', '4': '4', '5': '5', '6': '6', '7': '7', '8': '8', '9': '9',
'A': '10', 'B': '11', 'C': '12', 'D': '13', 'E': '14', 'F': '15',
Expand All @@ -62,12 +62,22 @@ describe('use case', function() {
R.split(''),
R.reverse,
R.map(R.prop(R.__, symbols)),
R.addIndex(R.map)(function(val, index) {
return posNotation([index, val]);
}),
R.addIndex(R.map)(fn.mapper(posNotation)),
R.sum
);

// Vanilla equivalent

// var hexToDec = function(hex) {
// return hex
// .toUpperCase()
// .split('')
// .reverse()
// .map(function(val) { return symbols[val]; })
// .map(fn.mapper(posNotation))
// .reduce(function(acc, val) { return acc + val; }, 0);
// };

it('hexadecimal to decimal', function() {
hexToDec('AB').should.be.exactly(171);

Expand Down

0 comments on commit 3f2818c

Please sign in to comment.