Skip to content

Commit

Permalink
Clever optimisations for or and and
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Dec 5, 2024
1 parent be387b2 commit affccc5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
14 changes: 6 additions & 8 deletions convex-core/src/main/cvx/convex/core/core.cvx
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,10 @@
(cond
(== n 0) true
(== n 1) (first exprs)
:else `(let [v# ~(first exprs)]
(cond v#
:else `(cond ~(first exprs)
~(cons 'and
(next exprs))
v#)))))
*result*))))

(defmacro or
^{:doc {:description ["Executes expressions in sequence, returning the first truthy value, or the last value if all were falsey (false or nil)."
Expand All @@ -281,12 +280,11 @@
(cond
(== n 0) nil
(== n 1) (first exprs)
:else `(let [v# ~(first exprs)]
(cond
v#
v#
:else `(cond
~(first exprs)
*result*
~(cons 'or
(next exprs)))))))
(next exprs))))))

;;;;;;;;;; `cond` variants

Expand Down
2 changes: 1 addition & 1 deletion convex-core/src/test/java/convex/core/lang/CoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4970,7 +4970,7 @@ public void testExpand() {

@Test
public void testExpand_1() {
assertEquals(read("(let [v# 1] (cond v# v# (or 2 3)))"), eval("(expand-1 '(or 1 2 3))"));
assertEquals(read("(cond 1 *result* (or 2 3))"), eval("(expand-1 '(or 1 2 3))"));
}

@Test
Expand Down

0 comments on commit affccc5

Please sign in to comment.