Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
* Fix a bug where tags in data readers were resolved as Vars within syntax quotes, rather than using standard data readers rules (#1129)
* Fix a bug where `keyword` and `symbol` functions did not treat string arguments as potentially namespaced (#1131)
* Fix a bug where `condp` would throw an exception if a result expression was `nil` (#1137)

## [v0.3.2]
### Added
Expand Down
26 changes: 15 additions & 11 deletions src/basilisp/core.lpy
Original file line number Diff line number Diff line change
Expand Up @@ -3565,7 +3565,11 @@
- ``test-expr :>> result-fn`` where :>> is a keyword literal

For the ternary expression clause, the unary ``result-fn`` will be called with the
result of the predicate."
result of the predicate.

A single final expression can be included at the end with no test expression which
will be returned if no other clause matches the predicate. If no default is provided
and no clause matches the predicate, a ``ValueError`` will be thrown."
[pred expr & clauses]
(when (seq clauses)
(let [test-expr (first clauses)
Expand All @@ -3574,16 +3578,16 @@
(let [result (first remaining)
remaining (rest remaining)]
(cond
(= result :>>) `(let [res# ~(list pred test-expr expr)]
(if res#
(~(first remaining) res#)
(condp ~pred ~expr ~@(rest remaining))))
result `(if ~(list pred test-expr expr)
~result
(condp ~pred ~expr ~@remaining))
:else (throw
(ex-info "expected result expression"
{:test test-expr}))))
(not (seq remaining)) `(throw
(python/ValueError
(str "Expected result expression for condp " {:test ~test-expr})))
(= result :>>) `(let [res# ~(list pred test-expr expr)]
(if res#
(~(first remaining) res#)
(condp ~pred ~expr ~@(rest remaining))))
:else `(if ~(list pred test-expr expr)
~result
(condp ~pred ~expr ~@remaining))))
test-expr))))

(defmacro declare
Expand Down
11 changes: 10 additions & 1 deletion tests/basilisp/test_core_macros.lpy
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,16 @@
(is (= :a (condp = "a"
"b" :b
"c" :c
:a))))
:a)))
(is (nil? (condp = "a"
"b" :b
"a" nil
"c" :c
:a)))
(is (thrown? python/ValueError
(condp = "a"
"b" :b
"c" :c))))

(testing "condp result function"
(is (= :a (condp some [1 8 10 12]
Expand Down