Every now and then I have a container like
Set(["foo"]), which contains only a single element and I want to extract it. Is there a function that does this somewhere? It should
- Throw an error, if the container is empty or has more then one element.
- only assume the iteration protocol, e.g. not use
If there is nothing official, the following does the trick, what would be a good place to add a PR for it?
function single(iter) s1 = start(iter) if done(iter, s1) throw(ArgumentError("iter $iter is empty.")) end item, s2 = next(iter, s1) if !done(iter, s2) throw(ArgumentError("iter $iter has more then one element.")) end item end