I was curious about the behavior of the string range of Crystal, so I investigated various things in other languages.
Perl
% perl -v | head -2 | tail -1
This is perl 5, version 14, subversion 2 (v5.14.2) built for x86_64-linux
Z
% perl -e 'print chr ++($foo = ord "Z");'
[
Z
% perl -e 'print ++($foo = "Z");'
AA
% perl -e 'print "A".."Z";'
ABCDEFGHIJKLMNOPQRSTUVWXYZ
% perl -e 'print "A".."z";'
ABCDEFGHIJKLMNOPQRSTUVWXYZ
This is intense.
% perl -e 'print "a".."Z";'
abcdefghijklmnopqrstuvwxyz
% perl -e 'print "ZZ".."z";'
%
Ruby
$ ruby -v
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin13]
Z
> "Z".ord.succ.chr
=> "["
Z
> "Z".succ
=> "AA"
("A".."z").to_a
=> ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "[", "\\", "]", "^", "_", "`", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
> ("A".."AA").to_a
=> ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "AA"]
> ("ZZ".."z").to_a
=> ["ZZ"]
Python
$ python -V
Python 3.4.2
Z
In [1]: x = ord("Z"); x += 1; chr(x)
Out[1]: '['
Can not.
There is no such syntax, but it can be written.
for x in range(ord("A"), ord("[")):
print(chr(x), end="")
#=> ABCDEFGHIJKLMNOPQRSTUVWXYZ
You can also use string.ascii_letters
with ʻimport string`, but only in uppercase and lowercase letters.
for x in string.ascii_letters:
print(x, end="")
#=> abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
Can not.
% dmd -v | head -1
DMD64 D Compiler v2.068.0
Z
import std.stdio;
void main()
{
char x;
(++(x = 'Z')).writeln;
}
% rdmd x.d
[
Z
import std.stdio;
import std.string;
void main()
{
"Z".succ.writeln;
}
% rdmd xx.d
AA
import std.stdio;
import std.range;
void main()
{
iota('A', '[').writeln;
}
% rdmd xxx.d
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Can not.
Crystal
% crystal -v
Crystal 0.7.7 [170f859](Sat Sep 5 02:53:51 UTC 2015)
Z
puts 'Z'.succ
% crystal x.cr
[
Z
puts "Z".succ
% crystal x.cr
AA
% cat xx.cr
p ('A'..'z').to_a
% crystal xx.cr
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
This is fine.
% cat xxx.cr
p ("A".."Z").to_a
% crystal xxx.cr
["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
Case of problem.
% cat xxx.cr
p ("A".."z").to_a
"A"-> "Z"-> "AA"-> "ZZ"-> "AAA"-> "AAA .."-> "ZZZ .." is called infinitely.
% crystal xxx.cr
^C
The implementation of the string range is quite different. I didn't put it here, but Julia and Elixir don't seem to implement the string range or the equivalent of String # succ
.
Recommended Posts