[Issue 4605] Wrong print of an int[string] aa

2012-04-23 Thread d-bugmail
http://d.puremagic.com/issues/show_bug.cgi?id=4605


Kenji Hara k.hara...@gmail.com changed:

   What|Removed |Added

 Status|NEW |RESOLVED
 Resolution||FIXED


--- Comment #6 from Kenji Hara k.hara...@gmail.com 2012-04-23 22:32:24 PDT ---
Associative array formatting was improved in 2.055.
https://github.com/D-Programming-Language/phobos/pull/126

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
--- You are receiving this mail because: ---


[Issue 4605] Wrong print of an int[string] aa

2011-06-22 Thread d-bugmail
http://d.puremagic.com/issues/show_bug.cgi?id=4605



--- Comment #4 from bearophile_h...@eml.cc 2011-06-22 14:39:31 PDT ---
This function (related to the coding Kata Word Chains) creates an associative
array where the keys are the start chars of words, and the values are sets of
words. For simplicity in D2 I have implemented the string sets as bool[string].


import std.stdio, std.string;

bool[string][char] foo(string[] names) {
typeof(return) result;
foreach (name; names)
result[name[0]][name] = true;
return result;
}

auto names = mary patricia linda barbara elizabeth jennifer
maria susan margaret dorothy lisa nancy karen betty helen
sandra donna carol ruth sharon michelle laura sarah
kimberly deborah jessica shirley cynthia angela melissa
brenda amy anna rebecca virginia kathleen pamela;

void main() {
writeln(foo(names.split()));
}


The original complete program didn't have to print this result, but there I
have created a bug, so I have had to print result, as I have done in this
reduced program. This is the printout:


p:patricia:true pamela:true l:linda:true lisa:true laura:true d:dorothy:true
donna:true deborah:true h:helen:true m:melissa:true margaret:true michelle:true
maria:true mary:true e:elizabeth:true a:angela:true anna:true amy:true
b:barbara:true betty:true brenda:true j:jessica:true jennifer:true n:nancy:true
r:ruth:true rebecca:true v:virginia:true s:sharon:true susan:true shirley:true
sandra:true sarah:true k:kathleen:true karen:true kimberly:true c:cynthia:true
carol:true


For me this is very bad, I am not able to read it well. Nesting of dictionaries
produces a hard to read output.

To better show what I mean this a Python2.6 translation (here I have used true
sets, that are built-in, but the situation doesn't change a lot):

from collections import defaultdict

def foo(names):
result = defaultdict(set)
for name in names:
result[name[0]].add(name)
return result

names = mary patricia linda barbara elizabeth jennifer
maria susan margaret dorothy lisa nancy karen betty helen
sandra donna carol ruth sharon michelle laura sarah
kimberly deborah jessica shirley cynthia angela melissa
brenda amy anna rebecca virginia kathleen pamela

print foo(names.split())


Its textual output allows me to tell apart sub-dictionaries:

defaultdict(type 'set', {'a': set(['amy', 'anna', 'angela']), 'c':
set(['carol', 'cynthia']), 'b': set(['barbara', 'betty', 'brenda']), 'e':
set(['elizabeth']), 'd': set(['dorothy', 'donna', 'deborah']), 'h':
set(['helen']), 'k': set(['kathleen', 'kimberly', 'karen']), 'j':
set(['jessica', 'jennifer']), 'm': set(['margaret', 'melissa', 'michelle',
'mary', 'maria']), 'l': set(['laura', 'linda', 'lisa']), 'n': set(['nancy']),
'p': set(['pamela', 'patricia']), 's': set(['sarah', 'sharon', 'sandra',
'shirley', 'susan']), 'r': set(['ruth', 'rebecca']), 'v': set(['virginia'])})


Using pprint (pretty print) from the Python standard library it improves:
from pprint import pprint
pprint(dict(foo(names.split(


{'a': set(['amy', 'angela', 'anna']),
 'b': set(['barbara', 'betty', 'brenda']),
 'c': set(['carol', 'cynthia']),
 'd': set(['deborah', 'donna', 'dorothy']),
 'e': set(['elizabeth']),
 'h': set(['helen']),
 'j': set(['jennifer', 'jessica']),
 'k': set(['karen', 'kathleen', 'kimberly']),
 'l': set(['laura', 'linda', 'lisa']),
 'm': set(['margaret', 'maria', 'mary', 'melissa', 'michelle']),
 'n': set(['nancy']),
 'p': set(['pamela', 'patricia']),
 'r': set(['rebecca', 'ruth']),
 's': set(['sandra', 'sarah', 'sharon', 'shirley', 'susan']),
 'v': set(['virginia'])}


This is even better:

{'a': {amy, angela, anna},
 'b': {barbara, betty, brenda},
 'c': {carol, cynthia},
 'd': {deborah, donna, dorothy},
 'e': {elizabeth},
 'h': {helen},
 'j': {jennifer, jessica},
 'k': {karen, kathleen, kimberly},
 'l': {laura, linda, lisa},
 'm': {margaret, maria, mary, melissa, michelle},
 'n': {nancy},
 'p': {pamela, patricia},
 'r': {rebecca, ruth},
 's': {sandra, sarah, sharon, shirley, susan},
 'v': {virginia}
}


If you want a more apples-to-apples comparison this is Python code that uses
the same data structure used by the D code:

from collections import defaultdict

def foo(names):
result = defaultdict(dict)
for name in names:
result[name[0]][name] = True
return result

names = mary patricia linda barbara elizabeth jennifer
maria susan margaret dorothy lisa nancy karen betty helen
sandra donna carol ruth sharon michelle laura sarah
kimberly deborah jessica shirley cynthia angela melissa
brenda amy anna rebecca virginia kathleen pamela

print foo(names.split())


Its textual output:

defaultdict(type 'dict', {'a': {'amy': True, 'anna': True, 'angela': True},
'c': {'carol': True, 'cynthia': True}, 'b': {'barbara': True, 'betty': True,
'brenda': True}, 'e': {'elizabeth': True}, 'd': {'dorothy': True, 'donna':
True, 'deborah': True}, 'h': {'helen': True}, 'k': {'kathleen': 

[Issue 4605] Wrong print of an int[string] aa

2010-08-30 Thread d-bugmail
http://d.puremagic.com/issues/show_bug.cgi?id=4605


Andrej Mitrovic andrej.mitrov...@gmail.com changed:

   What|Removed |Added

 CC||andrej.mitrov...@gmail.com


--- Comment #2 from Andrej Mitrovic andrej.mitrov...@gmail.com 2010-08-29 
18:47:36 PDT ---
(In reply to comment #0)
 This D2 program, compiled with dmd 2.048b:
 
 
 import std.stdio;
 void main() {
 int[string] aa = [10:10, 20:20];
 writeln(aa);
 }
 
 
 Prints (note the 1:10, that is a bug):
 20:20 1:10
 
 
 The expected less buggy and less barbaric output is:
 [10: 10, 20: 20]
 Or:
 [20: 20, 10: 10]

It get's even worse:

import std.stdio;
void main() {
int[string] aa = [100:1, 200:1, 300:1, 400:1];
writeln(aa);
}

Prints:

400:1 3:1 2:1 1:1

For your second code, I agree. I would prefer if D took an approach similar to
other languages and printed out something like:

[[5:6, 7:8]:2]

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
--- You are receiving this mail because: ---


[Issue 4605] Wrong print of an int[string] aa

2010-08-30 Thread d-bugmail
http://d.puremagic.com/issues/show_bug.cgi?id=4605



--- Comment #3 from bearophile_h...@eml.cc 2010-08-29 19:06:06 PDT ---
Whitespace is important for readability, so instead of:
[[5:6, 7:8]:2]

It's better to print a space after the colon:
[[5: 6, 7: 8]: 2]

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
--- You are receiving this mail because: ---


[Issue 4605] Wrong print of an int[string] aa

2010-08-11 Thread d-bugmail
http://d.puremagic.com/issues/show_bug.cgi?id=4605



--- Comment #1 from bearophile_h...@eml.cc 2010-08-11 14:52:18 PDT ---
This D2 code:

import std.stdio;
void main() {
int[int[int]] aa;
aa[[5:6, 7:8]] = 2;
writeln(aa);
}


gives the useless output:
5:6 7:8:2

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
--- You are receiving this mail because: ---