count not max-words

master
Matthew Butterick 5 years ago
parent 898bded70f
commit 6fe0bdba1c

@ -16,7 +16,7 @@
(define combo #false) (define combo #false)
(define min-size #false) (define min-size #false)
(define max-size #false) (define max-size #false)
(define max-words #false) (define count #false)
(define hide-plurals #true) (define hide-plurals #true)
(define proper-names #false) (define proper-names #false)
(command-line (command-line
@ -32,9 +32,9 @@
[("-c" "--combo") combo-arg [("-c" "--combo") combo-arg
"mandatory combo" "mandatory combo"
(set! combo combo-arg)] (set! combo combo-arg)]
[("-n" "--number") max-words-arg [("-n" "--number") count-arg
"max number of results" "max number of results"
(set! max-words (string->number max-words-arg))] (set! count (string->number count-arg))]
[("--min") min-size-arg [("--min") min-size-arg
"minimum word lengths" "minimum word lengths"
(set! min-size (string->number min-size-arg))] (set! min-size (string->number min-size-arg))]
@ -50,7 +50,7 @@
(displayln (string-join (make-words #:letters letters (displayln (string-join (make-words #:letters letters
#:mandatory mandatory #:mandatory mandatory
#:combo combo #:combo combo
#:max-words max-words #:count count
#:min min-size #:min min-size
#:max max-size #:max max-size
#:hide-plurals hide-plurals #:hide-plurals hide-plurals

@ -3,16 +3,17 @@
"index.rkt") "index.rkt")
(provide make-words) (provide make-words)
(define (make-words #:letters [letters "etaoinshrdluw"] (define (make-words #:letters [letters-arg #f]
#:mandatory [mandatory #f] #:mandatory [mandatory #f]
#:combo [combo #f] #:combo [combo #f]
#:min [min-length-arg 5] #:min [min-length-arg 5]
#:max [max-length-arg 10] #:max [max-length-arg 10]
#:hide-plurals [hide-plurals? #t] #:hide-plurals [hide-plurals? #t]
#:proper-names [proper-names? #f] #:proper-names [proper-names? #f]
#:max-words [max-words 10] #:count [count 10]
#:all-caps [all-caps? #f] #:all-caps [all-caps? #f]
#:initial-caps [initial-caps? #f]) #:initial-caps [initial-caps? #f])
(define letters (or letters-arg "abcdefghijklmnopqrstuvwxyz"))
(define mandatory-cs (define mandatory-cs
(if (or mandatory combo) (if (or mandatory combo)
(remove-duplicates (remove-duplicates
@ -35,13 +36,13 @@
(define min-length (or min-length-arg 0)) (define min-length (or min-length-arg 0))
(define max-length (or max-length-arg +inf.0)) (define max-length (or max-length-arg +inf.0))
(for*/fold ([word-acc null] (for*/fold ([word-acc null]
[count 0] [count-acc 0]
#:result word-acc) #:result word-acc)
([idx (in-list (shuffle (range (vector-length wordrecs))))] ([idx (in-list (shuffle (range (vector-length wordrecs))))]
[rec (in-value (vector-ref wordrecs idx))] [rec (in-value (vector-ref wordrecs idx))]
[word-charidx (in-value (word-rec-charint rec))] [word-charidx (in-value (word-rec-charint rec))]
[word (in-value (word-rec-word rec))] [word (in-value (word-rec-word rec))]
#:break (= count (or max-words +inf.0)) #:break (= count-acc (or count +inf.0))
#:when (and #:when (and
;; between min and max length ;; between min and max length
((if (<= min-length max-length) <= >=) min-length (word-rec-length rec) max-length) ((if (<= min-length max-length) <= >=) min-length (word-rec-length rec) max-length)
@ -60,10 +61,10 @@
;; maybe hide plurals ;; maybe hide plurals
(or (not hide-plurals?) (or (not hide-plurals?)
(not (word-rec-plural? rec))))) (not (word-rec-plural? rec)))))
(values (cons (caser word) word-acc) (add1 count)))) (values (cons (caser word) word-acc) (add1 count-acc))))
(module+ test (module+ test
(require rackunit) (require rackunit)
(time (make-words)) (time (make-words))
(check-equal? (sort (make-words #:mandatory "xyz" #:combo #false) string<?) (check-equal? (sort (make-words #:mandatory "xyz" #:combo #false #:letters "etaoinshrdluw") string<?)
'("azoxy" "dysoxidize" "isazoxy" "oxytonize" "rhizotaxy" "zootaxy"))) '("azoxy" "dysoxidize" "isazoxy" "oxytonize" "rhizotaxy" "zootaxy")))
Loading…
Cancel
Save