yary (>): > Speaking as a non-p6-coder "proposal sounds good to me" though the > spec raises some other questions. > >>The tr/// quote-like operator now also has a method form called >> trans(). Its argument is a list of pairs. You can use anything >> that produces a pair list: >> >> $str.trans( %mapping.pairs ); > > Does Perl 6 guarantee the order in which pairs are returned from a > hash? If not, then the following code won't always return the same > thing: > > my %mapping = 'a' => 'x', 'x' => 'b'; say 'ax'.trans(%mapping);
Perl 6 does not guarantee any particular order in which pairs are returned from a hash. However, this is a non-issue with .trans, since .trans doesn't care about the order. Not sure how clear it is from the spec, but .trans traverses things in *one* pass. That's the idea of it. So there's no conflict in the above example. 'a' goes to 'x' and 'x' goes to 'b', regardless of pair ordering. See <http://perl6advent.wordpress.com/2010/12/21/day-21-transliteration-and-beyond/> for deeper delvings into this. Even if I didn't see reasons why .trans shouldn't care about hash key ordering on the grounds of what it is in Perl 6, I would still be against it on the grounds of it being based on tr/// in Perl 5, which is on-pass and doesn't care about the ordering of characters on the left. > It might say 'bb' or 'xb', depending on how the pairs are returned. > That might be considered a programmer's error, but it seems > less-than-optimal that these two lines have the same result in my > somewhat dated Rakudo install: > >> say 'ax'.trans("a" => "x", "x" => "b") > xb >> say 'ax'.trans("x" => "b", "a" => "x") > xb > > - even if it is completely in accord with spec. No. As above. Perl 5 agrees: $ perl -wle '$_ = "ax"; tr/ax/xb/; print' xb $ perl -wle '$_ = "ax"; tr/xa/bx/; print' xb > And since mapping is a hash, it prevents a series of transl{ations > iterations} with the same "from" in a single call. And that's a feature. If you want a series of translations, make a series of .trans calls. (Unless my proposal goes through, in which case you should make a series of .translate calls.) // Carl