utils: 0-1 -> 01

Shorter and easier to type.
This commit is contained in:
Sergiu Ivanov 2020-06-06 08:23:55 +02:00
parent 8e6002040a
commit 180810a2aa
2 changed files with 11 additions and 11 deletions

View file

@ -574,7 +574,7 @@
;;; Pretty-prints a state of the network to Boolean values 0 or 1. ;;; Pretty-prints a state of the network to Boolean values 0 or 1.
(define (pretty-print-boolean-state s) (define (pretty-print-boolean-state s)
(string-join (hash-map s (λ (key val) (format "~a:~a" key (any->0-1 val))) #t))) (string-join (hash-map s (λ (key val) (format "~a:~a" key (any->01 val))) #t)))
(module+ test (module+ test
(test-case "pretty-print-boolean-state" (test-case "pretty-print-boolean-state"

View file

@ -49,8 +49,8 @@
[cartesian-product/stream (->* () #:rest (listof stream?) stream?)] [cartesian-product/stream (->* () #:rest (listof stream?) stream?)]
[boolean-power (-> number? (listof (listof boolean?)))] [boolean-power (-> number? (listof (listof boolean?)))]
[boolean-power/stream (-> number? (stream/c (listof boolean?)))] [boolean-power/stream (-> number? (stream/c (listof boolean?)))]
[any->0-1 (-> any/c (or/c 0 1))] [any->01 (-> any/c (or/c 0 1))]
[0-1->boolean (-> (or/c 0 1) boolean?)]) [01->boolean (-> (or/c 0 1) boolean?)])
;; Contracts ;; Contracts
(contract-out [variable-mapping? contract?] (contract-out [variable-mapping? contract?]
[string-variable-mapping? contract?] [string-variable-mapping? contract?]
@ -700,18 +700,18 @@
(check-equal? (stream->list (boolean-power/stream 2)) '((#f #f) (#f #t) (#t #f) (#t #t))))) (check-equal? (stream->list (boolean-power/stream 2)) '((#f #f) (#f #t) (#t #f) (#t #t)))))
;;; Converts any non-#f value to 1 and #f to 0. ;;; Converts any non-#f value to 1 and #f to 0.
(define (any->0-1 x) (if x 1 0)) (define (any->01 x) (if x 1 0))
(module+ test (module+ test
(test-case "any->0-1" (test-case "any->01"
(check-equal? (any->0-1 #t) 1) (check-equal? (any->01 #t) 1)
(check-equal? (any->0-1 #f) 0))) (check-equal? (any->01 #f) 0)))
;;; Converts 0 to #f and 1 to #t ;;; Converts 0 to #f and 1 to #t
(define (0-1->boolean x) (define (01->boolean x)
(case x [(0) #f] [else #t])) (case x [(0) #f] [else #t]))
(module+ test (module+ test
(test-case "0-1->boolean" (test-case "01->boolean"
(check-equal? (0-1->boolean 0) #f) (check-equal? (01->boolean 0) #f)
(check-equal? (0-1->boolean 1) #t))) (check-equal? (01->boolean 1) #t)))