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,26 +1,8 @@
;; -*- 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
(map
(lambda (l)
(let ((count (car l))
(direction (cadr l)))
(utest/map-comb
(count direction)
(utest/tb
((format "c~a_d~a" count direction)
"More complex testbench for Simple Counter"
@ -35,10 +17,8 @@
"simple_counter_tb"
#:parameters `((COUNT ,count)
(ITERATIONS ,(* count 3))
(DIRECTION ,direction))))))
(combinations
(DIRECTION ,direction))))
(append '(10 100 1000 16 64 256)
(let ((state (seed->random-state 0)))
(map (lambda (x) (+ 2 (random 200 state))) (iota 100))))
'(1 -1 0)))
'(1 -1 0))

View File

@ -967,6 +967,18 @@
((_ () 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
;;;