Add useful "map combinations" macro. See counter example

This commit is contained in:
Nikolay Puzanov 2022-08-20 16:36:48 +03:00
parent e667b0657d
commit 79f9ac5ad6
2 changed files with 18 additions and 26 deletions

View File

@ -1,27 +1,9 @@
;; -*- scheme -*- ;; -*- scheme -*-
;;; Make lists combinations
;;; Example: (combinations '(1 2 3) '(a b)) -> '((1 a) (1 b) (2 a) (2 b) (3 a) (3 b))
(define (combinations . lists)
(cond
((null? lists) '())
((null? (cdr lists)) (car lists))
(else
(fold (lambda (comb out)
(append out
(map (lambda (x)
(if (list? comb)
(cons x comb)
(list x comb)))
(car lists))))
'() (apply combinations (cdr lists))))))
;;; Testbenches ;;; Testbenches
(map (utest/map-comb
(lambda (l) (count direction)
(let ((count (car l)) (utest/tb
(direction (cadr l)))
(utest/tb
((format "c~a_d~a" count direction) ((format "c~a_d~a" count direction)
"More complex testbench for Simple Counter" "More complex testbench for Simple Counter"
(format "COUNT=~a\tDIRECTION=~a" count direction)) (format "COUNT=~a\tDIRECTION=~a" count direction))
@ -35,10 +17,8 @@
"simple_counter_tb" "simple_counter_tb"
#:parameters `((COUNT ,count) #:parameters `((COUNT ,count)
(ITERATIONS ,(* count 3)) (ITERATIONS ,(* count 3))
(DIRECTION ,direction)))))) (DIRECTION ,direction))))
(append '(10 100 1000 16 64 256)
(combinations
(append '(10 100 1000 16 64 256)
(let ((state (seed->random-state 0))) (let ((state (seed->random-state 0)))
(map (lambda (x) (+ 2 (random 200 state))) (iota 100)))) (map (lambda (x) (+ 2 (random 200 state))) (iota 100))))
'(1 -1 0))) '(1 -1 0))

View File

@ -967,6 +967,18 @@
((_ () body ...) ((_ () body ...)
(utest/tb (#f) body ...)))) (utest/tb (#f) body ...))))
;;;
;;; Map combinations macro
;;;
(define-syntax utest/map-comb
(syntax-rules ()
((_ (args ...) (body ...) lists ...)
(apply
map
(lambda (args ...) (body ...))
(transpose
(combinations lists ...))))))
;;; ;;;
;;; Delete working folders ;;; Delete working folders
;;; ;;;