Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement some-> macro #87

Merged
merged 3 commits into from
Mar 26, 2024
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 AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,4 @@
* Gabriel F. C. Pereira <[email protected]>
* Daniel Nagy <[email protected]>
* John Blundell <[email protected]>
* Artur Wroblewski <[email protected]>
1 change: 1 addition & 0 deletions NEWS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ New Features
------------------------------
* New macro `defmacro-kwargs`.
* New macro `parse-fn-params`.
* New macro `some->`.
* New function `sign`.
* New function `thru`.

Expand Down
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Reference
.. hy:automacro:: ->
.. hy:automacro:: ->>
.. hy:automacro:: as->
.. hy:automacro:: some->
.. hy:automacro:: doto

``collections`` — Tools for data structures
Expand Down
31 changes: 30 additions & 1 deletion hyrule/argmove.hy
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(import
hyrule.iterables [rest])
hyrule.iterables [rest]
itertools [chain])


(eval-and-compile
Expand Down Expand Up @@ -138,6 +139,34 @@
~name))


(defmacro some-> [head #* args]
"Thread `head` first through the `rest` of the forms, but short-circuit
if a form returns `None`.

``some->`` (or the *threading macro* with short-circuit) is used to avoid
nesting of expressions. The threading macro inserts each expression into
the next expression's first argument place. However if an expression
returns `None`, then following expressions are ignored. The following
code demonstrates this:

Examples:
::

=> (defn output [a b] (print a b))
=> (some-> (+ 4 6) (output 5))
10 5

=> (defn ret_none [a b] None)
=> (some-> 1 (+ 2) (ret_none 3) (* 3))
None
"
(setv val (hy.gensym))
`(cond (is (setx ~val ~head) None) None
~@(chain.from_iterable (gfor node args
[`(is (setx ~val (hy.R.hyrule.-> ~val ~node)) None) None]))
True ~val))


(defmacro doto [form #* expressions]
"Perform possibly mutating `expressions` on `form`, returning resulting obj.

Expand Down
33 changes: 32 additions & 1 deletion tests/test_argmove.hy
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(require hyrule [-> ->> as-> doto])
(import unittest [mock])
(require hyrule [-> ->> as-> some-> doto])


(defn test-threading []
Expand Down Expand Up @@ -63,6 +64,36 @@
"Sir Joseph Cooke Verco")))


(defn test-threading-some []
; the macro uses `cond`, so test for odd and even number of expressions
(assert (= (some-> 1 (+ 2) (* 3)) 9))
(assert (= (some-> 1 (+ 2) (* 3) (+ 1)) 10))

; test for non-expression form
(defn inc [x] (+ x 1))
(assert (= (some-> 1 inc) 2)))


(defn test-threading-some-circuit []
(setv m (mock.MagicMock))

; test for null head
(assert (is (some-> None m) None))
(assert (= m.call_count 0)) ;; m is never called

; test short-circuit with a function for odd and even number of
; expressions
(defn ret_none [a b] None)

(setv m (mock.MagicMock))
(assert (is (some-> 1 m (ret_none 3) m) None))
(assert (= m.call_count 1)) ;; m is called once only, before `ret_none`

(setv m (mock.MagicMock))
(assert (is (some-> 1 m m (ret_none 3) m) None))
(assert (= m.call_count 2))) ;; m is called twice only, before `ret_none`


(defn test-doto []
(setv collection [])
(doto collection (.append 1) (.append 2) (.append 3))
Expand Down