Last active
December 20, 2015 12:18
-
-
Save evincarofautumn/6129527 to your computer and use it in GitHub Desktop.
Using pattern matching to do pattern matching
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
// Tests whether a string matches a file glob pattern, in | |
// which '*' matches any number of characters, '?' matches | |
// a single character, and anything else matches itself. | |
def matches: | |
->{ string pattern } | |
if (string pattern {isEmpty} on &&): true | |
else: | |
false | |
string peel ->{ ms ss } | |
option ms: ->s | |
pattern peel ->{ mp ps } | |
option mp: ->p | |
if (p '*' eqChar): | |
drop | |
(string ps matches) | |
{ss pattern matches} | |
||| | |
else if ([s, '?'] p elemChar): | |
drop | |
ss ps matches | |
"foo.jpg" "f*.jp*g" matches sayBool // true | |
"foo.jpg" "*.jp?" matches sayBool // true | |
"foo.jpg" "g*.jp?" matches sayBool // false | |
// Splits a vector safely into head and tail. | |
def peel: | |
->v | |
if (v isEmpty): | |
none [] | |
else: | |
(v head) some | |
(v tail) | |
// Lazy logical OR. | |
def |||: | |
->{ x f } | |
if x: true else: f@ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment