From: Timothy J. H. <tim...@ma...> - 2004-06-25 00:34:18
|
Nice example! Maybe we should create a webpage of Jscheme snippets on the jscheme.sourceforge.net site. This one could be titled something like * generate a sort list of selected fields from tab-separated file of records... Here's an example I wrote today. Its a servlet that generates a webpage showing all of the symbols that are defined If you call it with a "which" parameters, e.g. show_bindings.servlet?new it will filter out all of the primitives, all of the javadot, and all those symbols defined in elf/basic.scm ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; show_bindings.servlet (servlet (new) (define js (jscheme.JScheme.)) (define mod (.eval js '(use-module "elf/basic.scm"))) (define (getBindings env) (.rep$# env)) (define jsenv (.INTERACTION_ENVIRONMENT$ (.evaluator$# js))) (define primitives (getBindings jsenv)) (define currentbindings (getBindings (.INTERACTION_ENVIRONMENT$ (jsint.Scheme.currentEvaluator)))) (define newbindings (filter (lambda(x) (or (equal? which #null) (and (equal? #null (.get primitives x)) (= -1 (.indexOf (.toString x) ".")) ))) (map* (lambda(x) x) (.keys currentbindings)))) (define (show-bindings) {<ol>[ (map* (lambda(x){<li>[x] </li>\n}) (sort newbindings (comparator string<? .toString))) ] </ol>}) (show-bindings) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; This is somewhat fragile code however since it uses the jsint package and things may change more rapidly in jsint than in jscheme (witness the disappearance of jsint.Version.java!) ---Tim--- On Jun 23, 2004, at 7:10 PM, Ken Anderson wrote: > I wrote this piece of code today and it struck me: > > (define (readCountryData) > (sort > (remove-duplicates > (readingTabData > (File. "../dafif/Dafift/AppC/appc_cc_icao.txt") > (lambda (country country_name icao_rgn usage) > (list country country_name)))) > (comparator string<? car))) > > (define (readingTabData file f) > (map* (lambda (r) (apply f (vector->list (.split r "\t")))) > (BufferedReader. (FileReader. file)))) > > Now, this code can be improved in a lot of ways, but it did what i > wanted - grind over a tab separated file, extract the fields i wanted, > and sort the result. It also abstracted out readinging from a tab > separated file so it can be resused. > > While you might argue that i should have produced an iterator to be > Java compatible, to do it in Java, you'd need an interface with > methods call and apply, and each of the 4 loops would require at least > 50% more lines of code. > > > > ------------------------------------------------------- > This SF.Net email sponsored by Black Hat Briefings & Training. > Attend Black Hat Briefings & Training, Las Vegas July 24-29 - > digital self defense, top technical experts, no vendor pitches, > unmatched networking opportunities. Visit www.blackhat.com > _______________________________________________ > Jscheme-user mailing list > Jsc...@li... > https://lists.sourceforge.net/lists/listinfo/jscheme-user |