From 1cac3c77a96acd09f2c950e9e5f5b3df7fab2924 Mon Sep 17 00:00:00 2001 From: David Florness Date: Tue, 10 Mar 2020 13:21:33 -0600 Subject: [PATCH] Simplify readline parsing code --- bulletin.rkt | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/bulletin.rkt b/bulletin.rkt index 6cecfcc..1b65e45 100644 --- a/bulletin.rkt +++ b/bulletin.rkt @@ -1,5 +1,6 @@ #lang racket/base (require racket/contract + (only-in racket/format ~a) racket/list racket/match racket/random @@ -234,14 +235,10 @@ (match (read sp) ['add (match (read sp) ['cand - (let* ([cand (read sp)] - [cand (if (symbol? cand) - (symbol->string cand) - cand)]) - (hash-update! election->candidates - election - (λ (l) - (cons cand l))))] + (hash-update! election->candidates + election + (λ (l) + (cons (~a (read sp)) l)))] [(? eof-object?) (displayln "add what? options are `cand'")] [else (displayln "cannot add that")])] ['list (match (read sp) @@ -250,17 +247,14 @@ ['peers (println (hash-values _peers))] ['elections - (displayln (format "current: ~a" election)) + (displayln (format "current: ~s" election)) (println (hash-keys election->candidates))] [(? eof-object?) (displayln "list what? options are `cands' `peers' `elections'")] [else (displayln "cannot list that")])] ['set (match (read sp) - ['election (let* ([election (read sp)] - [election (if (string? election) - (string->symbol election) - election)]) - (election-set! election))] + ['election (election-set! + (string->symbol (~a (read sp))))] [(? eof-object?) (displayln "set what? options are `election'")] [else (displayln "cannot set that")])] @@ -275,14 +269,10 @@ (transition 'registering) (reset)] ['kick (if (eq? state 'closed) - (let* ([peer-uname (read sp)] - [peer-uname (if (symbol? peer-uname) - (symbol->string peer-uname) - peer-uname)]) - (for ([(token peer) _peers] - #:when (equal? (hash-ref peer 'username) - peer-uname)) - (hash-remove! _peers token))) + (for ([(token peer) _peers] + #:when (equal? (hash-ref peer 'username) + (~a (read sp)))) + (hash-remove! _peers token)) (displayln "close the election first"))] ['state (displayln (symbol->string state))] [else (displayln "unknown command")]) -- 2.38.4