Skip to content

Commit

Permalink
removed STRING and NUMBER
Browse files Browse the repository at this point in the history
  • Loading branch information
santinic committed Dec 15, 2018
1 parent ff7089b commit 252d3d0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 33 deletions.
33 changes: 15 additions & 18 deletions lib/pampy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
const PAD_VALUE = Symbol('PadValueType');
const _ = Symbol('UnderscoreType');
const ANY = _;
const STRING = Symbol('StringType');
const NUMBER = Symbol('NumberType');
const HEAD = Symbol('HeadType');
const TAIL = Symbol('TailType');
const REST = TAIL;
Expand Down Expand Up @@ -36,29 +34,26 @@ function run(action, x) {


function matchValue(patt, value) {
if(patt === '_') {
if (patt === '_') {
// Behaves like UnderscoreType
return [true, [value]];
}
else if (isValue(patt)) {
return [patt === value, []]
}
else if (patt === PAD_VALUE) {
return [false, []];
}
else if (patt === STRING) {
let bool = typeof(value) === 'string' || value instanceof String;
if (bool) return [bool, [value]];
else return [false, []];
else if (patt === String) {
let bool = typeof(value) === 'string' || value instanceof String;
if (bool) return [bool, [value]];
else return [false, []];
}
else if (patt === NUMBER) {
else if (patt === Number) {
let bool = typeof(value) === 'number' || value instanceof Number;
if (bool) return [bool, [value]];
else return [false, []];
}
else if (patt === Array) {
if (value instanceof Array) {
return [true, [value]];
return [true, [value]];
}
else return [false, []];
}
Expand All @@ -72,7 +67,11 @@ function matchValue(patt, value) {
return [true, [value]];
return [false, []];
}
catch (err) {}
catch (err) {
}
}
else if (isValue(patt)) {
return [patt === value, []]
}
else if (patt instanceof Function) {
// console.log(`[${patt}] instanceof Function`);
Expand Down Expand Up @@ -159,14 +158,14 @@ function matchDict(patt, value) {
}
}
if (!matchedLeftAndRight)
return [false, []];
return [false, []];
}
return [true, totalExtracted];
}

function pairwise(args) {
let res = [];
for (let i = 0; i < args.length; i+=2) {
for (let i = 0; i < args.length; i += 2) {
res.push([args[i], args[i + 1]]);
}
return res;
Expand Down Expand Up @@ -202,7 +201,7 @@ function match(x) {

function matchAll(rows) {
let total = [];
for(let i=0; i < rows.length; i++) {
for (let i = 0; i < rows.length; i++) {
let row = rows[i];
let pairs = [...arguments].slice(1);
let res = match.apply(null, [row].concat(pairs));
Expand Down Expand Up @@ -237,8 +236,6 @@ module.exports = {
matchAll,
zipLongest,
PAD_VALUE,
STRING,
NUMBER,
_,
ANY,
HEAD,
Expand Down
30 changes: 15 additions & 15 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ let assert = require('chai').assert;
let lodash = require('lodash');
let fs = require('fs');
let {matchArray, matchValue, matchDict, zipLongest, match, matchAll, _, HEAD, TAIL, REST,} = require('../lib/pampy');
let {PAD_VALUE, STRING, NUMBER, ANY, MatchError} = require('../lib/pampy');
let {PAD_VALUE, ANY, MatchError} = require('../lib/pampy');


describe('matchValue', () => {
Expand All @@ -19,10 +19,10 @@ describe('matchValue', () => {
assert.deepEqual(matchValue(_, 1), [true, [1]])
});
it('types', () => {
assert.deepEqual(matchValue(STRING, "ok"), [true, ["ok"]]);
assert.deepEqual(matchValue(STRING, 3), [false, []]);
assert.deepEqual(matchValue(NUMBER, 3), [true, [3]]);
assert.deepEqual(matchValue(NUMBER, "ok"), [false, []]);
assert.deepEqual(matchValue(String, "ok"), [true, ["ok"]]);
assert.deepEqual(matchValue(String, 3), [false, []]);
assert.deepEqual(matchValue(Number, 3), [true, [3]]);
assert.deepEqual(matchValue(Number, "ok"), [false, []]);
});
});
describe('matchArray', () => {
Expand All @@ -39,8 +39,8 @@ describe('matchArray', () => {
assert.deepEqual(matchArray([1, _, _], [1, 2, 3]), [true, [2, 3]]);
assert.deepEqual(matchArray([1, _, 3], [1, 2, 3]), [true, [2]]);

assert.deepEqual(matchArray([1, NUMBER, 3], [1, 2, 3]), [true, [2]]);
assert.deepEqual(matchArray([1, STRING, NUMBER], [1, "2", 3]), [true, ["2", 3]]);
assert.deepEqual(matchArray([1, Number, 3], [1, 2, 3]), [true, [2]]);
assert.deepEqual(matchArray([1, String, Number], [1, "2", 3]), [true, ["2", 3]]);
});
it('nested', () => {
assert.deepEqual(matchArray([1, [_, 3], _], [1, [2, 3], 4]), [true, [2, 4]])
Expand All @@ -59,7 +59,7 @@ describe('matchDict', () => {
});
it('ambiguous double _', () => {
assert.deepEqual(matchDict({a: _, _: _}, {a: 1, b: 2}), [true, [1, 'b', 2]]);
assert.deepEqual(matchDict({a: STRING, _: _}, {a: "1", b: 2}), [true, ["1", 'b', 2]]);
assert.deepEqual(matchDict({a: String, _: _}, {a: "1", b: 2}), [true, ["1", 'b', 2]]);
});
});
describe('match', () => {
Expand All @@ -71,10 +71,10 @@ describe('match', () => {
assert.throws(() => match([1,2,3] [TAIL, 1], 1), MatchError);
});
it('ignored case', () => {
assert.throws(() => match(3, STRING, "ok"), MatchError);
assert.throws(() => match(3, String, "ok"), MatchError);
});
it('lambda args', () => {
assert.equal(match(3, NUMBER, (x) => x), 3);
assert.equal(match(3, Number, (x) => x), 3);
assert.equal(match([1, 2], [1, _], (x) => x), 2);
assert.equal(match([1, 2, 3], [_, 2, 3], (x) => x), 1);
assert.deepEqual(match([1, 2, 3], [_, _, 3], (a, b) => [a, b]), [1, 2]);
Expand All @@ -87,16 +87,16 @@ describe('match', () => {
it('lambda cond', () => {
function f(x) {
return match(x,
STRING, (x => `${x} is a string`),
String, x => `${x} is a String`,
x => x > 3, x => `${x} is > 3`,
x => x < 3, x => `${x} is < 3`,
x => x === 3, x => `${x} is = 3`);
}
assert.equal(f(3), "3 is = 3");
assert.equal(f(2), "2 is < 3");
assert.equal(f(4), "4 is > 3");
assert.equal(f("hello"), "hello is a string");
assert.equal(f("3"), "3 is a string");
assert.equal(f("hello"), "hello is a String");
assert.equal(f("3"), "3 is a String");
});
it('fibonacci', () => {
function fib(n) {
Expand Down Expand Up @@ -169,9 +169,9 @@ describe('match', () => {
return match(x,
null, "null",
undefined, "undefined",
NUMBER, "number");
Number, "Number");
}
assert.equal(f(3), "number");
assert.equal(f(3), "Number");
assert.equal(f(null), "null");
let z;
assert.equal(f(z), "undefined");
Expand Down

0 comments on commit 252d3d0

Please sign in to comment.