-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Add only
function
#25078
Add only
function
#25078
Conversation
The function `only(x)` returns the one-and-only element of a collection `x`, or else throws an error.
Handy, but do note it's a feature so it could be added at any time. |
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.
I think this would be nice, for the expressive power for anyone reading the code (for places where currently we use first
or [1]
, as Andy mentioned. For example, dot = only([2 3] * [1; 2])
)
Returns the one and only element of collection `x`, and throws an error if the collection | ||
has zero or multiple elements. | ||
""" | ||
Base.@propagate_inbounds function only(x) |
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.
It seems odd to mark this this way, where @inbounds only(x)
is first(x)
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.
The oddness here comes because I want @inbounds
to propagate to next
, since that can have @boundscheck
s in it.
Also, isn't julia> x = [1]
1-element Array{Int64,1}:
1
julia> x[]
1
julia> push!(x, 2)
2-element Array{Int64,1}:
1
2
julia> x[]
WARNING: omitting indices for non-singleton trailing dimensions is deprecated. Add `1` as trailing indices or use `reshape` to make the dimensionality of the array match the number of indices.
Stacktrace:
[...]
1 |
Only for arrays, this is for any iterable. |
We could make it work for any iterable, using this definition 😀 |
You mean using I guess, I definitely had thought of that, but had decided against it for now. Personally, I'd like to avoid conflating the iteration API with the indexing API any more than necessary, until this has been more thoroughly thought through. The relationship between iterable containers and indexable containers is already challenging - for example, with a one-element (If we were to link indexing and iteration more strongly in the future, we could revisit this). |
Going the other way - I'd have something like (It makes sense, since then |
Is there intend to include this feature into @assert length(iter) == 1; first(iter) dozens of times in different places. So at least for me this would be very useful. See also here. |
I'm happy to rebase this. There remains some TODOs:
|
The default for empty version idea sounds good. I also used quick and dirty variants of it a few times. For the name, at least |
I suggest |
A singleton is a set with one element though, and this will not return that, this will return the element of a "singleton" (although not only a |
Nah, lets not over-complicate things. The usecase of: "I am fairly sure i have narrowed this down to 1 thing" is very clear. |
There is some precedence for having a default: |
Just an observation: in Python, there's an idiom to assign the first element, and assert it's the only one. In [1]: (x,) = [3]
In [2]: (x,) = [3,4]
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-2-b3aec10ab953> in <module>()
----> 1 (x,) = [3,4]
ValueError: too many values to unpack (expected 1) This doesn't work in Julia, however
|
What is the status of this? I'd find this feature useful :) |
@nickrobinson251 probably best to just start a new PR since htis is so far behind head. |
Seems like a useful feature. I would like to generalize it to |
What would it return? for what you desribe, I can only assume it would return an iterator. |
Yes, recall it returns a single value, not a collection (it’s for unwrapping collections containing only one thing). |
|
Added in #33129 🎉 |
The function
only(x)
returns the one-and-only element of a collectionx
, or else throws an error.I discovered this little gem in LINQ (under the
IEnumerable
methodx.Single()
), and instantly fell in love. In particular, it's quite useful when you have e.g. filtered down some data and expect only one element, but it feels dirty to callx[1]
orfirst(x)
without at least checking that it has one (and only one) element. This little function lets you grab the element and make the assertion that it is the only element, all in a few characters.(Note: this is currently a part of my SplitApplyCombine.jl experiment, which has strategies to deal with nested containers, but to me seems a much better fit for
Base
/Base.Iterators
, which deals with... iteration...)