Skip to content

Commit

Permalink
when-list-or-empty helper macro
Browse files Browse the repository at this point in the history
  • Loading branch information
Paradiesstaub committed Jan 20, 2019
1 parent 2cdac99 commit 8dde593
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
9 changes: 9 additions & 0 deletions doc/reference/misc.md
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,15 @@ Removes one layer of a nested proper list.

Removes all nested layers of a proper list.

### when-list-or-empty
::: tip usage
```
(when-list-or-empty list body ...)
```

Macro which evaluates the body only if the passed value is
a non-empty list, otherwise an empty list is returned.



## LRU caches
Expand Down
5 changes: 4 additions & 1 deletion src/std/misc/list-test.ss
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,7 @@
(check-equal? (flatten1 '(1 (2) (3) ((4)))) '(1 2 3 (4)))
(check-equal? (flatten1 '((1) ((2)) 3)) '(1 (2) 3))
(check-equal? (flatten1 '(1 2 ())) '(1 2))
(check-equal? (flatten1 '(1 2 (()))) '(1 2 ())))))
(check-equal? (flatten1 '(1 2 (()))) '(1 2 ())))
(test-case "test when-list-or-empty"
(check-equal? (when-list-or-empty [1] "a") "a")
(check-equal? (when-list-or-empty [] "a") []))))
11 changes: 10 additions & 1 deletion src/std/misc/list.ss
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ package: std/misc
for-each!
push!
flatten
flatten1)
flatten1
when-list-or-empty)

;; This function checks if the list is a proper association-list.
;; ie it has the form [[key1 . val1] [key2 . val2]]
Expand Down Expand Up @@ -189,3 +190,11 @@ package: std/misc
(else (cons v acc))))
[]
list-of-lists))

;; Macro which evaluates the body only if the passed value is
;; a non-empty list, otherwise an empty list is returned.
(defrules when-list-or-empty ()
((_ list body body* ...)
(if (null? list)
[]
body body* ...)))
2 changes: 1 addition & 1 deletion src/std/run-tests.ss
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"misc/list-test"
"misc/channel-test"
"misc/lru-test"
"misc/func-test"
"misc/func-test"
"misc/queue-test"
"misc/deque-test"
"misc/pqueue-test"
Expand Down

0 comments on commit 8dde593

Please sign in to comment.