Last active
August 29, 2015 14:00
-
-
Save danking/11101463 to your computer and use it in GitHub Desktop.
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
#lang shill/cap | |
require shill/contracts | |
shill/io; | |
provide [find : forall [X:+path,+lookup,+contents] . [f: X] | |
[filter: [_: X] -> boolean?] | |
[cmd: [_: X] -> any] | |
-> | |
any ]; | |
# filter functions | |
provide [name : [_: string?] -> [_ : pathable] -> boolean?] | |
[fullname : [_: string?] -> [_ : pathable] -> boolean?]; | |
# actions | |
provide [grep : [regexp : or/c(string?, regexp?)] | |
[out : writeable?] -> | |
[file : file/c(+read, +path)] | |
-> | |
any ]; | |
val pathable = or/c(file/c(+path),dir/c(+path)); | |
val ls = fun(dir) { contents(dir); }; | |
val find = fun(f, filter, cmd) { | |
if and(file?(f),filter(f)) then | |
cmd(f); | |
# if f is a directory, recurse on its contents | |
if dir?(f) then | |
for g in ls(f) do { | |
val fc = lookup(f,g); | |
if not(sys-error?(fc)) then | |
find(fc, filter, cmd); | |
}; | |
}; | |
val grep = fun(regexp, out) { | |
fun (file) { | |
val contents = read(file); | |
val matches = regexp-match*(regexp, contents); | |
for match in matches do { | |
fwrite(out, "~a: ~a\n", path(file), match); | |
} | |
}; | |
}; | |
val name = fun(r) { | |
val re = regexp(r); | |
fun(f) regexp-match?(re, shortname(path(f))); | |
}; | |
val shortname = fun(path) { | |
path-element->string(last(explode-path(string->path(path)))); | |
}; | |
val fullname = fun(r) { | |
val re = regexp(r); | |
fun(f) regexp-match?(re, path(f)); | |
}; |
Author
danking
commented
Apr 20, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment