it's time to zoom out and look at what we've done so far.
we can get a bunch of INFORMATION on any term we want to bite off.
we can get all of this INFORMATION at once. this language tries to bite off "pasta" and emits EVERYTHING we know about it.
TRANSLATED:
it's easy. we [just] make our translate function go through all of the functions we've looked at so far.
function translate(text) {
return {
check: checkPasta(text),
bite: bitePasta(text),
tail: tailPasta(text),
travel: travelPasta(text),
error: errorPasta(text),
}
}
as we know, checkPasta is the easiest.
function checkPasta(text) {
return text.startsWith("pasta")
}
for some of the others, we can now use checkPasta to help us
out.
function bitePasta(text) {
if (checkPasta(text)) {
return text.slice(0, 5)
}
return null
}
function tailPasta(text) {
if (checkPasta(text)) {
return text.slice(5)
}
return null
}
function errorPasta(text) {
if (!checkPasta(text)) {
return `TRAGIC error: expected 'pasta' but found '${text.slice(0, 5)}'!`
}
return null
}
travelPasta stays the same as before.
function travelPasta(text) {
let i = 0
while (i < text.length) {
if (text[i] !== "pasta"[i]) {
break
}
i++
}
return text.slice(0, i)
}
back to the dream