Last active
August 29, 2015 14:09
-
-
Save CMCDragonkai/75530c81af135344094b to your computer and use it in GitHub Desktop.
OMetaJS: Repeat Higher Order
This file contains hidden or 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
| 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"] |
This file contains hidden or 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
| 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