#lang racket (require setup/dirs) ;; f-in: input-port? ;; file: path? (define-syntax-rule (try-read-file file f-in body ...) (let ([f-in (open-input-file file)]) (with-handlers ([exn:fail? (λ(e) (displayln (exn-message e)) (close-input-port f-in) #f)]) (begin0 (begin body ...) (close-input-port f-in))))) ;; f-in: input-port? (define (read-all f-in) (let loop ([s (read f-in)] [l '()]) (if (eof-object? s) l (loop (read f-in) (cons s l))))) ;; file: path? (define (read-rkt-file file) (or (try-read-file file f-in (and (read-language f-in (λ _ #f)) (read-all f-in))) (try-read-file file f-in (read-all f-in)))) ;; h: dict? ;; tree: any/c (define (count-identifiers h tree) (cond [(symbol? tree) (dict-set! h tree (+ 1 (dict-ref h tree 0)))] [(list? tree) (for-each (λ(t)(count-identifiers h t)) tree)] ; else nothing )) (define id-hash (make-hash)) ;; skips files that are difficult to read (for ([f (in-directory (find-collects-dir))]) (when(equal? (filename-extension f) #"rkt") ;(printf "Processing file: ~a~n" f) (define all (read-rkt-file f)) (if all (count-identifiers id-hash all) (printf "Could not read file: ~a\n" f)))) (define id-list (hash->list id-hash)) ; multiply the number of occurrences by the number of characters (define id-list-sorted (sort id-list > #:key (λ(p)(* (string-length (symbol->string (car p))) (cdr p))))) ;cdr)) ; the first most used identifiers w.r.t. their length * occurrences (take id-list-sorted 300)