Skip to content

Instantly share code, notes, and snippets.

@CMCDragonkai
Last active August 29, 2015 14:09
Show Gist options
  • Select an option

  • Save CMCDragonkai/75530c81af135344094b to your computer and use it in GitHub Desktop.

Select an option

Save CMCDragonkai/75530c81af135344094b to your computer and use it in GitHub Desktop.
OMetaJS: Repeat Higher Order
ometa Test {
repeat :n :p = ?(n == 0) -> ('') // base case
| apply(p):l repeat(n - 1, p):r -> ((r.length > 0) ? [l].concat(r) : [l]), // concat the recursive matches
tom = "TOM",
repeatTOM = repeat(3, 'tom')
}
Test.matchAll(
'TOMTOMTOM',
'repeatTOM'
);
// result: ["TOM", "TOM", "TOM"]
ometa Repetition {
tom = "TOM",
top = repeat("tom", 4)
}
Repetition.repeat = function(rule, count) {
var ret = [];
for(var i=0; i<count; i++) {
ret.push(this._apply(rule));
}
return ret;
};
Repetition.matchAll(
"TOMTOMTOMTOM",
"top"
);
// result: ["TOM", "TOM", "TOM", "TOM"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment