-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #237 from theqabalist/master
Support Fantasy-Land Contravariant interface (and Profunctor by simple extension)
- Loading branch information
Showing
4 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
"use strict"; | ||
|
||
suite("contramap", function() { | ||
function toLower(x) { | ||
return x.toLowerCase(); | ||
} | ||
|
||
function chrs(b) { | ||
return b.toString("ascii"); | ||
} | ||
|
||
test("with a function, transforms the input and parses that", function() { | ||
var parser = Parsimmon.string("x").contramap(function(x) { | ||
return x.toLowerCase(); | ||
}); | ||
assert.deepEqual(parser.parse("X"), { status: true, value: "x" }); | ||
}); | ||
|
||
test("asserts that a function was given", function() { | ||
assert.throws(function() { | ||
Parsimmon.string("x").contramap("not a function"); | ||
}); | ||
}); | ||
|
||
test("upholds contravariant law of composition", function() { | ||
var parser1 = Parsimmon.string("a") | ||
.contramap(toLower) | ||
.contramap(chrs); | ||
var parser2 = Parsimmon.string("a").contramap(function(x) { | ||
return toLower(chrs(x)); | ||
}); | ||
var input = Buffer.from([0x61]); | ||
|
||
assert.deepEqual(parser1.parse(input), parser2.parse(input)); | ||
}); | ||
|
||
test("embedded contramaps make sense", function() { | ||
var parser = Parsimmon.seq( | ||
Parsimmon.string("a"), | ||
Parsimmon.seq(Parsimmon.string("c"), Parsimmon.string("d")) | ||
.tie() | ||
.contramap(function(x) { | ||
return x.slice(1); | ||
}) | ||
).tie(); | ||
|
||
assert.deepEqual(parser.parse("abcd"), { status: true, value: "acd" }); | ||
}); | ||
|
||
test("backtracking with contramaps works", function() { | ||
var parser = Parsimmon.seq( | ||
Parsimmon.string("a"), | ||
Parsimmon.seq(Parsimmon.string("c"), Parsimmon.string("d")) | ||
.tie() | ||
.contramap(function(x) { | ||
return x.slice(1); | ||
}) | ||
) | ||
.tie() | ||
.or(Parsimmon.all); | ||
|
||
assert.deepEqual(parser.parse("abcde"), { status: true, value: "abcde" }); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
"use strict"; | ||
|
||
suite("promap", function() { | ||
function toLower(x) { | ||
return x.toLowerCase(); | ||
} | ||
|
||
function ord(chr) { | ||
return chr.charCodeAt(0); | ||
} | ||
|
||
function chrs(b) { | ||
return b.toString("ascii"); | ||
} | ||
|
||
function length(x) { | ||
return x.length; | ||
} | ||
|
||
function sub1(x) { | ||
return x - 1; | ||
} | ||
|
||
test("with a function, transforms the input and parses that, and transforms the output", function() { | ||
var parser = Parsimmon.string("a").promap(toLower, ord); | ||
assert.deepEqual(parser.parse("A"), { status: true, value: 0x61 }); | ||
}); | ||
|
||
test("asserts that a function was given", function() { | ||
assert.throws(function() { | ||
Parsimmon.string("x").promap("not a function", toLower); | ||
}); | ||
|
||
assert.throws(function() { | ||
Parsimmon.string("x").promap(toLower, "not a function"); | ||
}); | ||
}); | ||
|
||
test("upholds profunctor law of composition", function() { | ||
var parser1 = Parsimmon.string("aa") | ||
.promap(toLower, length) | ||
.promap(chrs, sub1); | ||
|
||
var parser2 = Parsimmon.string("aa").promap( | ||
function(x) { | ||
return toLower(chrs(x)); | ||
}, | ||
function(x) { | ||
return sub1(length(x)); | ||
} | ||
); | ||
var input = Buffer.from([0x41, 0x41]); | ||
|
||
assert.deepEqual(parser1.parse(input), parser2.parse(input)); | ||
}); | ||
}); |