Skip to content

Commit

Permalink
Make lif treat False as false
Browse files Browse the repository at this point in the history
  • Loading branch information
Kodiologist committed Dec 15, 2024
1 parent 0fe9fa6 commit b0912f7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
1 change: 1 addition & 0 deletions NEWS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Breaking Changes
------------------------------
* `recur` is now a real object that must be imported from Hyrule when
using `loop`.
* `lif` now treats `False` as false.

New Features
------------------------------
Expand Down
18 changes: 9 additions & 9 deletions hyrule/control.hy
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,10 @@
(defmacro lif [#* args]
#[[A "Lispy if" similar to :hy:func:`if` and :hy:func:`cond
<hy.core.macros.cond>`. Its most notable property is that it tests
the condition with ``(is-not condition None)`` instead of ``(bool
condition)``, so values such as the integer 0, the empty string, and
``False`` are considered true, not false. The general
syntax is
the condition values against ``None`` and ``False`` with
:hy:func:`is-not <hy.pyops.is-not>`, rather than calling
:py:class:`bool`. Thus, values such as the integer 0 and the empty
string are considered true, not false. The general syntax is
::
(lif
Expand All @@ -220,12 +220,12 @@
::
(cond
(is-not condition1 None) result1
(is-not condition2 None) result2
(is-not None condition1 False) result1
(is-not None condition2 False) result2
True else-value)
True else-value)
When no condition matches and there is no else-value, the result is ``None``.]]
When no condition obtains and there is no else-value, the result is ``None``.]]

(_lif args))

Expand All @@ -235,7 +235,7 @@
(get args 0)
args (do
(setv [condition result #* rest] args)
`(if (is-not ~condition None)
`(if (is-not None ~condition False)
~result
~(_lif rest)))))

Expand Down
5 changes: 3 additions & 2 deletions tests/test_control.hy
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,12 @@
(defn test-lif []
;; None is false
;; `None` and `False` are false
(assert (= (lif None "true" "false") "false"))
(assert (= (lif False "true" "false") "false"))
;; But everything else is True! Even falsey things.
(for [x [True False 0 "some-string" "" (+ 1 2 3)]]
(for [x [True 0 "some-string" "" (+ 1 2 3)]]
(assert (= (lif x "true" "false") "true")))
;; Test ellif [sic]
Expand Down

0 comments on commit b0912f7

Please sign in to comment.