Type any->01.

This commit is contained in:
Sergiu Ivanov 2022-03-05 12:55:23 +01:00
parent 357fcad89f
commit 687aea5337
2 changed files with 22 additions and 11 deletions

View file

@ -488,3 +488,13 @@ Like @racket[boolean-power], but returns a stream.
@examples[#:eval utils-evaluator @examples[#:eval utils-evaluator
(stream->list (boolean-power/stream 2)) (stream->list (boolean-power/stream 2))
]} ]}
@defproc[(any->01 [x Any]) (U Zero One)]{
Converts any non-@racket[#f] value to 1 and @racket[#f] to 0.
@examples[#:eval utils-evaluator
(any->01 #t)
(any->01 #f)
(any->01 'hello)
]}

View file

@ -25,7 +25,7 @@
update-vertices/unweighted update-graph dotit collect-by-key update-vertices/unweighted update-graph dotit collect-by-key
collect-by-key/sets ht-values/list->set hash->list/ordered collect-by-key/sets ht-values/list->set hash->list/ordered
multi-split-at lists-transpose in-random cartesian-product-2/stream multi-split-at lists-transpose in-random cartesian-product-2/stream
cartesian-product/stream boolean-power boolean-power/stream) cartesian-product/stream boolean-power boolean-power/stream any->01)
(define-type Variable Symbol) (define-type Variable Symbol)
(define-type (VariableMapping A) (Immutable-HashTable Variable A)) (define-type (VariableMapping A) (Immutable-HashTable Variable A))
@ -541,6 +541,15 @@
(module+ test (module+ test
(test-case "boolean-power/stream" (test-case "boolean-power/stream"
(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)))))
(: any->01 (-> Any (U Zero One)))
(define (any->01 x)
(if x 1 0))
(module+ test
(test-case "any->01"
(check-equal? (any->01 #t) 1)
(check-equal? (any->01 #f) 0)))
) )
(require 'typed) (require 'typed)
@ -552,14 +561,13 @@
update-vertices/unweighted update-graph dotit collect-by-key update-vertices/unweighted update-graph dotit collect-by-key
collect-by-key/sets ht-values/list->set hash->list/ordered collect-by-key/sets ht-values/list->set hash->list/ordered
multi-split-at lists-transpose in-random cartesian-product-2/stream multi-split-at lists-transpose in-random cartesian-product-2/stream
cartesian-product/stream boolean-power boolean-power/stream) cartesian-product/stream boolean-power boolean-power/stream any->01)
;;; Untyped section. ;;; Untyped section.
(provide (provide
;; Functions ;; Functions
(contract-out [any->01 (-> any/c (or/c 0 1))] (contract-out [01->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?]
@ -584,13 +592,6 @@
;;; Boolean operations ;;; Boolean operations
;;; ================== ;;; ==================
;;; Converts any non-#f value to 1 and #f to 0.
(define (any->01 x) (if x 1 0))
(module+ test
(test-case "any->01"
(check-equal? (any->01 #t) 1)
(check-equal? (any->01 #f) 0)))
;;; Converts 0 to #f and 1 to #t ;;; Converts 0 to #f and 1 to #t
(define (01->boolean x) (define (01->boolean x)