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
FEAT: clip function #5194
base: master
Are you sure you want to change the base?
FEAT: clip function #5194
Conversation
Although one thing we should consider is the One option is to accept either a range or a block with expressions that reduces to Of course, we could just write |
Just for the record, I've recently added a feature to View allowing to define a bounding box when using dragging mode:
That feature implementation could benefit from such |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All good!
A remark: the docstring should perhaps be "... its range boundary" not "... it range boundary" |
It's "nearest to it (to A) range boundary". Perhaps replace |
Or rather "the boundary nearest to A" ? |
I will post thoughts in a bit. I also had some comments for the |
Arg NamingThere is some precedent for
Of course we all love how it makes code shorter. I have a Arg Order
Sometimes we don't care about the order, but most of the time order is important. Often crucially. Here's my old
Here my old laziness comes out, as there are no type specs, so the internal comparison will leak out on errors. :^\
Thanks for the super cool example @hiiamboris. 👍 What my brain says, when I read the above code, is "Wait, what's being clipped? Why is the order different?" I get that it's an example to show the lack of ordering, but it's not compelling, because it's confusing code compared to, say:
I'm open to compelling arguments of course. What we also have to reconcile is what the doc string says with whether we promote order independence. I'll take another step, though, and ask what if we want the query version of
Next up: The Domain... |
I did a quick search for things I have related to this domain (as I see it):
Bounds-Range(old stuff) I also found an experimental
with a question about the implied lower bound for dates (01-Jan-0000 ?) Side Note: I want to bring aggregators into the mix too, but that's peripheral to this domain, with some conceptual overlap. So I won't. :^) |
Thanks, @greggirwin
That's exactly the moment one should experience to free oneself from the notion of order necessity. So, expected, and explained. But for most users there's a docstring which they can consult with or memorize, and your "compelling example" is what I would write when possible. I'm still using Another bad example:
Yes it does. And we have Inclusive |
Agreed 100% on tradeoffs.
Except that order is necessary most of the time. So this is the broader question in Red's design. One of those things we may have to try and see how it works in the real world. |
I just thought, if there's no graceful way we can resolve the 2-arity/3-arity problem, maybe we can have both, e.g. Another thought: 2-arity |
FYI other practical examples of
|
Re: argument naming. Of course I thought of that. But I don't see how |
On
That seems like a very specialized case, which will make the common cases much harder to reason about.
Indeed.
They at least hint at the order, and are explicitly meaningless.
|
Let's also consider how dependent/constrained types might be represented. I don't remember where @rebolek's experiment was, but I'm thinking of how you'd define them ( |
This is why I have so much mezz code that I haven't PR'd. Even the simplest things lead to deep conversations, and we all need to work through them in a timely manner or thoughts get stale. |
Open ranges are a must have if we have closed ones, so provided we figure a good syntax for that, there will be no need in specifying just lower or higher bound. And that page confirms that |
It's interesting that I recently tinkered on another func where the arg order raised questions for me.
|
Just 2 thoughts for the record: |
Yes, |
I prefer named arguments that imply an order, as it would make it easier to remember and read for everyone. That does not preclude people from using it in different order if they want to confuse others. ;-) I let you choose the names. For the function name, |
After using ternary I can confirm segment intersection need comes up from time to time, e.g. if I want to extract text fragment
to both filter out non-intersecting ranges and offset those that do intersect. Same applicable to N-dimensional points. As to whether we should use binary
But in this example I test specifically for zero intersection and throw it away. |
What would you think if you saw in someone's code:
v: max min a b min max a b v
?Probably that poor fellow has gone bananas.
However
v: clip v a b
is easy to understand: "clip value V between A and B".Thought I'd PR this function which I find very useful in many projects, and this particular design is the result of it's long usage, and doesn't interfere with any other planned features that I'm aware of.
For the ease of understanding of it's meaning and use, as well as docstring formulation, docstring implies that
A
is the point, and[B,C]
is the segment.Yet the function has a nice property: one doesn't have to remember the order of arguments, because it doesn't matter! If A,B,C are scalars, it chooses one of A,B,C that is between the other two. If A,B,C are pairs, it applies the same logic to both X and Y axes. If types are mixed, result is at the mercy of
min
/max
functions, most likely type promotion first, then the same logic follows.An illustration: