On 2013-05-05, at 22:19 , Noam Yorav-Raphael wrote:
> Hello,
>
> My name is Noam Yorav-Raphael. I find Rust to be a really exciting language!
>
> I have a simple suggestion: the current implementation of zip() returns an
> iterator which stops whenever one of the two iterators it gets stop.
> I use zip() in python quite a bit. I always have a few lists, where the
> i'th value in each corresponds to the same thing. I use zip in python to
> iterate over a few of those lists in parallel.
>
> I think this is the usual use case. In this use case, when the two lists
> have a different length it means that I have a bug. it seems to me that
> Python's behavior, and current Rust behavior, is contrary to "Errors should
> never pass silently" from the zen of Python.
>
> What do you think of changing this, so that zip() will fail in such a case?
> Another iterator, say, "zipcut" can implement the current behavior if
> needed.
>
> It will be my pleasure to implement this, if the suggestion is found useful.
FWIW there are languages falling either way, Haskell's zip is "shortest":
Prelude> zip [1..3] [1..4]
[(1,1),(2,2),(3,3)]
Prelude> zip [1..5] [1..4]
[(1,1),(2,2),(3,3),(4,4)]
while erlang's is strict:
1> lists:zip([1, 2, 3], [1, 2]).
** exception error: no function clause matching lists:zip([3],[])
(lists.erl, line 372)
in function lists:zip/2 (lists.erl, line 372)
in call from lists:zip/2 (lists.erl, line 372)
2> lists:zip([1, 2, 3], [1, 2, 3, 4]).
** exception error: no function clause matching lists:zip([],[4])
(lists.erl, line 372)
in function lists:zip/2 (lists.erl, line 372)
in call from lists:zip/2 (lists.erl, line 372)
I've never had issues one way or an other, but note one thing: zip
"shortest" is the most useful one when the language has infinite
sequences (infinite lazy lists in Haskell, infinite generators/iterators
in Python) as it allows zipping an infinite and a finite and get a
sensible (and terminating) result, e.g.
zip(itertools.count(), collection)
thus it is not an error to do it that way but an extremely useful property.
Because Erlang's lists are strict and finite, an assertion of equal length
makes sense: there are no infinite streams to zip to, zipping of lists of
different length can pretty much only be a bug.
Now here's the question, to which I don't have an answer but which will
tell you whether your suggestion makes sense — at least when compared
to existing languages: is it possible to have an infinite vector in Rust,
or to zip finite and infinite datastructures (iterators?) together?
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev