~edwargix/tallyard

d114617a0d6d61ab3689049c06cf7122f8ecfd05 — David Florness 5 years ago c772cdd
The ballot TUI returns a list of the candidates in the proper order
1 files changed, 18 insertions(+), 5 deletions(-)

M ballot-tui.rkt
M ballot-tui.rkt => ballot-tui.rkt +18 -5
@@ 1,12 1,14 @@
#lang racket/base

(require racket/list)

(require charterm)

(define (get-vote candidates)
  (let ([n (length candidates)]
        [prefs (make-hash (for/list ([cand candidates])
                            (cons cand #f)))]
        [data-line 2])
  (let* ([n (length candidates)]
         [prefs (make-hash (for/list ([cand candidates])
                             (cons cand n)))]
         [data-line 2])
    (with-charterm
      (charterm-clear-screen)
      (for ([i (in-naturals data-line)]


@@ 41,7 43,18 @@
                    (case keycode
                      [(return)
                       (if (>= cursor-line n)
                           prefs
                           (let ([prefs (sort (hash->list prefs) < #:key cdr)])
                             (let loop ([p   (car prefs)]
                                        [rst (cdr prefs)])
                               (let-values
                                   ([(same-rank rst)
                                     (partition (λ (e)
                                                  (eqv? (cdr e) (cdr p)))
                                                rst)])
                                 (cons (cons (car p) (map car same-rank))
                                       (if (empty? rst)
                                           empty
                                           (loop (car rst) (cdr rst)))))))
                           (loop-fast-next-key cursor-line))]
                      [(ctrl-c)
                       (raise exn:break)]