Skip to content

Commit

Permalink
Rename && to ands
Browse files Browse the repository at this point in the history
  • Loading branch information
gilch committed Dec 24, 2023
1 parent d912814 commit e2a4591
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 43 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ which includes Clojure-like persistent data structures.
```EDN
0 ; from garden_of_edn import _this_file_as_main_; """#"
(hissp/_macro_.prelude)
(attach _macro_ . ands #hissp/$"_macro_.&&")

(defmacro #hissp/$"m#" t (tuple (.extend [(quote pyrsistent/m) (quote .)] t)))
(defmacro #hissp/$"j#" j (complex 0 j))

Expand Down
2 changes: 1 addition & 1 deletion docs/style_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1031,7 +1031,7 @@ if it's not obvious from the identifier.

.. code-block:: Lissp
"``&&`` 'and'. Like Python's ``and`` operator, but for any number of arguments."
"``@#`` 'decorator' applies ``decoration`` to a global and reassigns."
This way, all three name versions (munged, demunged, and pronounced)
will appear in generated docs.
Expand Down
55 changes: 28 additions & 27 deletions src/hissp/macros.lissp
Original file line number Diff line number Diff line change
Expand Up @@ -1524,44 +1524,45 @@ Hidden doctest adds bundled macros for REPL-consistent behavior.
(get#0 $#stack))))

;; I would have named this 'and, but that's a reserved word.
(defmacro && (: :* exprs)
"``&&`` 'and'. Shortcutting logical AND.
(defmacro ands (: :* exprs)
"Variadic shortcutting logical AND.

Returns the first false value, otherwise the last value.
There is an implicit initial value of ``True``.

.. code-block:: REPL

#> (&& True True False) ; and finds the False
>>> # QzET_QzET_
#> (ands True True False) ; and finds the False
>>> # ands
... (lambda x0,x1,x2:x0 and x1()and x2())(
... True,
... (lambda :True),
... (lambda :False))
False

#> (&& False (print 'oops)) ; Shortcutting.
>>> # QzET_QzET_
#> (ands False (print 'oops)) ; Shortcutting.
>>> # ands
... (lambda x0,x1:x0 and x1())(
... False,
... (lambda :
... print(
... 'oops')))
False

#> (&& True 42)
>>> # QzET_QzET_
#> (ands True 42)
>>> # ands
... (lambda x0,x1:x0 and x1())(
... True,
... (lambda :(42)))
42

#> (&&)
>>> # QzET_QzET_
#> (ands)
>>> # ands
... True
True

#> (&& 42)
>>> # QzET_QzET_
#> (ands 42)
>>> # ands
... (42)
42

Expand Down Expand Up @@ -1618,7 +1619,7 @@ Hidden doctest adds bundled macros for REPL-consistent behavior.
... ()
()

See also: `&&<QzET_QzET_>`, `bool`, `or`.
See also: `ands`, `bool`, `or`.
"
(cond (op#eq (len exprs) 1) (get#0 exprs)
exprs
Expand Down Expand Up @@ -1678,7 +1679,7 @@ Hidden doctest adds bundled macros for REPL-consistent behavior.
... (lambda _Qz2IKKUCBWz_G=(lambda _Qz2IKKUCBWz_x:
... # hissp.macros.._macro_.ifQz_else
... (lambda b,c,a:c()if b else a())(
... # hissp.macros.._macro_.QzET_QzET_
... # hissp.macros.._macro_.ands
... (lambda x0,x1:x0 and x1())(
... __import__('builtins').isinstance(
... _Qz2IKKUCBWz_x,
Expand Down Expand Up @@ -1707,8 +1708,8 @@ Hidden doctest adds bundled macros for REPL-consistent behavior.
See also: `throw`, `throw*<throwQzSTAR_>`.
"
`(throw* (let ($#G (lambda ($#x)
(if-else (&& (isinstance $#x type)
(issubclass $#x BaseException))
(if-else (ands (isinstance $#x type)
(issubclass $#x BaseException))
($#x) $#x)))
(attach ($#G ,exception) : ,'__cause__ ($#G ,cause)))))

Expand Down Expand Up @@ -1848,8 +1849,8 @@ Hidden doctest adds bundled macros for REPL-consistent behavior.
;;
;; See also: `X# <XQzHASH_>`.
;;
(if-else (&& (op#is_ str (type f))
(.startswith f "."))
(if-else (ands (op#is_ str (type f))
(.startswith f "."))
`(lambda ($#self : :* $#xs)
(,f $#self $#xs))
`(lambda (: :* $#xs)
Expand Down Expand Up @@ -1879,7 +1880,7 @@ Hidden doctest adds bundled macros for REPL-consistent behavior.
;;
;; See also: `#<QzHASH_>`, `%<QzPCENT_>`.
;;
(when (&& xs (op#eq :* (get#-1 xs)))
(when (ands xs (op#eq :* (get#-1 xs)))
(throw (SyntaxError "trailing :*")))
(let (ixs (iter xs))
`((lambda (: :* ,'xs) .#"[*xs]")
Expand Down Expand Up @@ -1910,7 +1911,7 @@ Hidden doctest adds bundled macros for REPL-consistent behavior.

See also: `@<QzAT_>`, `%<QzPCENT_>`.
"
(when (&& xs (op#eq :* (get#-1 xs)))
(when (ands xs (op#eq :* (get#-1 xs)))
(throw (SyntaxError "trailing :*")))
(let (ixs (iter xs))
`((lambda (: :* ,'xs) .#"{*xs}")
Expand Down Expand Up @@ -2148,7 +2149,7 @@ Hidden doctest adds bundled macros for REPL-consistent behavior.
;; ... (lambda _Qz2IKKUCBWz_G=(lambda _Qz2IKKUCBWz_x:
;; ... # hissp.macros.._macro_.ifQz_else
;; ... (lambda b,c,a:c()if b else a())(
;; ... # hissp.macros.._macro_.QzET_QzET_
;; ... # hissp.macros.._macro_.ands
;; ... (lambda x0,x1:x0 and x1())(
;; ... __import__('builtins').isinstance(
;; ... _Qz2IKKUCBWz_x,
Expand Down Expand Up @@ -2729,10 +2730,10 @@ except ModuleNotFoundError:pass"
3.1622776601683795

"
(if-else (&& e (op#is_ tuple (type e)))
(if-else (ands e (op#is_ tuple (type e)))
(let-from (sym : :* args) e
(if-else (&& (op#is_ str (type sym))
(re..search ".[:^]" (hissp..demunge sym)))
(if-else (ands (op#is_ str (type sym))
(re..search ".[:^]" (hissp..demunge sym)))
(._rewrite _macro_
(re..findall "([/&@<>*:]|(?:[^,^`/&@<>*:]|`[,^/&@<>*:])+)(,?\^*)"
(hissp..demunge sym))
Expand All @@ -2750,11 +2751,11 @@ except ModuleNotFoundError:pass"
: reader (hissp..reader.Lissp : ns (.get hissp.compiler..NS))
is? XY#(op#is_ X (type Y))
str? X#(my.is? str X)
startswith? XY#(&& (my.str? X) (.startswith X Y))
startswith? XY#(ands (my.str? X) (.startswith X Y))
literal? X#(ors (op#eq () X) (not (op#contains (# tuple str) (type X))))
quotation? X#(&& (my.is? tuple X) (op#eq 'quote (get#0 X)))
quotation? X#(ands (my.is? tuple X) (op#eq 'quote (get#0 X)))
control-word? X#(my.startswith? X ":")
module-handle? X#(&& (my.str? X) (.endswith X "."))
module-handle? X#(ands (my.str? X) (.endswith X "."))
method? X#(my.startswith? X ".")
exprs (list exprs)
arity (.count suffix "^")
Expand Down
28 changes: 14 additions & 14 deletions tests/test_macros.lissp
Original file line number Diff line number Diff line change
Expand Up @@ -116,23 +116,23 @@
(operator..not_ (operator..mod i 7)))
(self.assertEqual xs (enlist 1 2 3 4 5 6 7))))

test_&&
test_ands
(lambda (self)
(*#let (xs (list))
(*#doto xs
(.append (*#&&))
(.append (*#&& 0))
(.append (*#&& 1))
(.append (*#&& 0 (.append xs :oops)))
(.append (*#&& 1 (.append xs 2)))
(.append (*#&& True
(.append xs 3)
(.append xs :oops)))
(.append (*#&& True
(*#progn (.append xs 4)
:oops)
(.append xs 5)))
(.append (*#&& 1 2 (*#progn (.append xs 6) 7))))
(.append (*#ands))
(.append (*#ands 0))
(.append (*#ands 1))
(.append (*#ands 0 (.append xs :oops)))
(.append (*#ands 1 (.append xs 2)))
(.append (*#ands True
(.append xs 3)
(.append xs :oops)))
(.append (*#ands True
(*#progn (.append xs 4)
:oops)
(.append xs 5)))
(.append (*#ands 1 2 (*#progn (.append xs 6) 7))))
(self.assertEqual (enlist True 0 1 0 2 None 3 None 4 5 None 6 7)
xs)))

Expand Down

0 comments on commit e2a4591

Please sign in to comment.