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

Implement some-> macro #87

merged 3 commits into from
Mar 26, 2024

Conversation

wrobell
Copy link

@wrobell wrobell commented Mar 23, 2024

Thread head first through the rest of the forms, but short-circuit if a form returns None.

This is an attempt to progress with #5 ticket.

@Kodiologist
Copy link
Member

You're assuming that -> exists in the scope where some-> expands:

$ echo '(require hyrule [some->]) (some-> 1 (+ 2))' | hy
NameError: name 'hyx_XhyphenHminusXXgreaterHthan_signX' is not defined

So you should refer to -> as hy.R.hyrule.-> instead.

steps and steps_none are used only once, so you can inline them. In fact, you can provide both parts in one lfor and skip the zip entirely.

"un-even" is better written "odd".

A test of short-circuiting should check that no side-effects occur of forms that are supposed to be skipped. I typically do this by trying to append to a list and then checking that the list is still empty.

The intended property of some-> that the thread ends when one of the arguments itself evaluates to None, as in (setv x None) (some-> 1 x (+ 2)), appears to be untested.

@wrobell
Copy link
Author

wrobell commented Mar 23, 2024

Thanks for the feedback.

BTW. Do you think it would make sense to extract the processing of a form (use of _dotted, and if statement for expression) into separate macro, and then reuse it in both -> and some->?

@Kodiologist
Copy link
Member

I'm not sure. You could try it and see if the result is neater. Subroutines of macros are generally better written as functions than macros, though.

@wrobell
Copy link
Author

wrobell commented Mar 23, 2024

The intended property of some-> that the thread ends when one of the arguments itself evaluates to None, as in (setv x None) (some-> 1 x (+ 2)), appears to be untested.

From https://clojuredocs.org/clojure.core/some-%3E (emphasis mine)

threads it into the first form (via ->), and when that result is not nil, through the next

(setv x None) would be equivalent to some-> 1 None (+ 2)) and would result in TypeError: 'NoneType' object is not callable, which is expected result?

BTW. There is the following test, which I have extended a bit (commit 9b30918)

(defn test-threading-some-circuit []
  (defn ret_none [a b] None)
  (assert (is (some-> 1 (+ 2) (ret_none 3) (* 3)) None)))

@wrobell
Copy link
Author

wrobell commented Mar 23, 2024

Feedback tasks

@wrobell
Copy link
Author

wrobell commented Mar 23, 2024

steps and steps_none are used only once, so you can inline them. In fact, you can provide both parts in one lfor and skip the zip entirely.

Originally I created steps as a tuple of (form, None). This was hard to follow.

What if we get rid of zip with

  (setv steps (gfor node args 
                     ...))
  ; interleave each form with None to use them with `cond` macro below
  (setv steps (gfor v steps #(v None)))

Please see commit 9b30918 (and a small fix 2a35619).

@wrobell
Copy link
Author

wrobell commented Mar 23, 2024

A test of short-circuiting should check that no side-effects occur of forms that are supposed to be skipped. I typically do this by trying to append to a list and then checking that the list is still empty.

IMHO, this is better done with mocking, please see commit ec9317e.

@Kodiologist
Copy link
Member

What if we get rid of zip with…

Still seems overcomplicated to me. See my new commit.

Otherwise, looks good. I guess I misunderstood how some-> is supposed to work. I didn't even know Python's built-in mock library existed, so, good to know. Add a NEWS item and update the manual and this should be done.

@Kodiologist
Copy link
Member

And don't forget to add yourself to AUTHORS.

@wrobell
Copy link
Author

wrobell commented Mar 24, 2024

Add a NEWS item and update the manual and this should be done.

Done via commit 57b33cb.

Anything else to correct, let me know please. Otherwise, for summary, please see #87 (comment).

Copy link

@davidahern davidahern left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably i shouldn't have permission to do this

@Kodiologist
Copy link
Member

Good point. That should be fixed now.

@wrobell
Copy link
Author

wrobell commented Mar 24, 2024

@Kodiologist @scauligi i have asked my friend to approve as it seems the settings are wide open. i suspect this is not intended?

@Kodiologist
Copy link
Member

Yes, I was under the impression that GitHub actually didn't let you restrict reviewing privileges, but now, at least, it does.

wrobell and others added 3 commits March 26, 2024 11:53
Thread `head` first through the `rest` of the forms, but short-circuit
if a form returns `None`.
@Kodiologist Kodiologist merged commit 8004c84 into hylang:master Mar 26, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants