diff --git a/scribblings/utils.scrbl b/scribblings/utils.scrbl index de6a67c..1718815 100644 --- a/scribblings/utils.scrbl +++ b/scribblings/utils.scrbl @@ -498,3 +498,12 @@ Converts any non-@racket[#f] value to 1 and @racket[#f] to 0. (any->01 #f) (any->01 'hello) ]} + +@defproc[(01->boolean [x (U Zero One)]) Boolean]{ + +Converts 0 to @racket[#f] and 1 to @racket[#t]. + +@examples[#:eval utils-evaluator +(01->boolean 0) +(01->boolean 1) +]} diff --git a/utils.rkt b/utils.rkt index 11eee5b..a0233bc 100644 --- a/utils.rkt +++ b/utils.rkt @@ -25,7 +25,8 @@ update-vertices/unweighted update-graph dotit collect-by-key collect-by-key/sets ht-values/list->set hash->list/ordered multi-split-at lists-transpose in-random cartesian-product-2/stream - cartesian-product/stream boolean-power boolean-power/stream any->01) + cartesian-product/stream boolean-power boolean-power/stream any->01 + 01->boolean) (define-type Variable Symbol) (define-type (VariableMapping A) (Immutable-HashTable Variable A)) @@ -550,6 +551,15 @@ (test-case "any->01" (check-equal? (any->01 #t) 1) (check-equal? (any->01 #f) 0))) + + (: 01->boolean (-> (U Zero One) Boolean)) + (define (01->boolean x) + (case x [(0) #f] [else #t])) + + (module+ test + (test-case "01->boolean" + (check-equal? (01->boolean 0) #f) + (check-equal? (01->boolean 1) #t))) ) (require 'typed) @@ -561,13 +571,12 @@ update-vertices/unweighted update-graph dotit collect-by-key collect-by-key/sets ht-values/list->set hash->list/ordered multi-split-at lists-transpose in-random cartesian-product-2/stream - cartesian-product/stream boolean-power boolean-power/stream any->01) + cartesian-product/stream boolean-power boolean-power/stream any->01 + 01->boolean) ;;; Untyped section. (provide - ;; Functions - (contract-out [01->boolean (-> (or/c 0 1) boolean?)]) ;; Contracts (contract-out [variable-mapping? contract?] [string-variable-mapping? contract?] @@ -586,18 +595,3 @@ (define (general-pair/c key-contract val-contract) (or/c (list/c key-contract val-contract) (cons/c key-contract val-contract))) - - -;;; ================== -;;; Boolean operations -;;; ================== - - -;;; Converts 0 to #f and 1 to #t -(define (01->boolean x) - (case x [(0) #f] [else #t])) - -(module+ test - (test-case "01->boolean" - (check-equal? (01->boolean 0) #f) - (check-equal? (01->boolean 1) #t)))