diff --git a/scribblings/utils.scrbl b/scribblings/utils.scrbl index f6ff7a6..88f057c 100644 --- a/scribblings/utils.scrbl +++ b/scribblings/utils.scrbl @@ -477,4 +477,13 @@ Returns the @racket[n]-th Cartesian power of the Boolean domain. @examples[#:eval utils-evaluator (boolean-power 2) -]} \ No newline at end of file +]} + +@defproc[(boolean-power/stream [n Integer]) + (Sequenceof (Listof Boolean))]{ + +Like @racket[boolean-power], but returns a stream. + +@examples[#:eval utils-evaluator +(stream->list (boolean-power/stream 2)) +]} diff --git a/utils.rkt b/utils.rkt index 50c0633..c71ad62 100644 --- a/utils.rkt +++ b/utils.rkt @@ -25,7 +25,7 @@ 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) + cartesian-product/stream boolean-power boolean-power/stream) (define-type Variable Symbol) (define-type (VariableMapping A) (Immutable-HashTable Variable A)) @@ -534,6 +534,13 @@ (module+ test (test-case "boolean-power" (check-equal? (boolean-power 2) '((#f #f) (#f #t) (#t #f) (#t #t))))) + + (: boolean-power/stream (-> Integer (Sequenceof (Listof Boolean)))) + (define (boolean-power/stream n) (cartesian-product/stream (make-list n '(#f #t)))) + + (module+ test + (test-case "boolean-power/stream" + (check-equal? (stream->list (boolean-power/stream 2)) '((#f #f) (#f #t) (#t #f) (#t #t))))) ) (require 'typed) @@ -545,14 +552,13 @@ 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) + cartesian-product/stream boolean-power boolean-power/stream) ;;; Untyped section. (provide ;; Functions - (contract-out [boolean-power/stream (-> number? (stream/c (listof boolean?)))] - [any->01 (-> any/c (or/c 0 1))] + (contract-out [any->01 (-> any/c (or/c 0 1))] [01->boolean (-> (or/c 0 1) boolean?)]) ;; Contracts (contract-out [variable-mapping? contract?] @@ -578,14 +584,6 @@ ;;; Boolean operations ;;; ================== -;;; Like boolean-power, but returns a stream whose elements the -;;; elements of the Cartesian power. -(define (boolean-power/stream n) (cartesian-product/stream (make-list n '(#f #t)))) - -(module+ test - (test-case "boolean-power/stream" - (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. (define (any->01 x) (if x 1 0))