<|"a" -> 1, "b" -> 2, "c" -> 3, "d" -> 4|>[[1]]
gives values
1
while
<|"a" -> 1, "b" -> 2, "c" -> 3, "d" -> 4|>[[1;;2]]
gives
<|"a" -> 1, "b" -> 2|>
I just feel an inconsistency in this kind of Part design.
Why
<|"a" -> 1, "b" -> 2, "c" -> 3, "d" -> 4|>[[1]]
doesn't give
"a" -> 1
as
{"a" -> 1, "b" -> 2, "c" -> 3, "d" -> 4}[[1]]
gives
"a" -> 1
or as many people point out that functions act transparently on Association's values, why doesn't
<|"a" -> 1, "b" -> 2, "c" -> 3, "d" -> 4|>[[1;;2]]
give
{1,2}
??? Can somebody give an explanation?
(key->value), one must use[[n;;n]]syntax or[[n;;]]. But if you use[[n]]this will only give the value at partn. The reason why[[n;;m]]work for bothkey,valuesat bothnthroughmparts, is because of the;;usage. So to pull both key+value, use;;, else it will default to only the value at that part. – Nasser Dec 13 '14 at 04:55<|"a" -> 1, "b" -> 2|>[[{1, 2}]]also behaves asSpandoes. My guess is that the creator assumed that someone who usesSpanis collecting several elements. i.e. the intention is to make this change when the user requests more than one element. – C. E. Dec 13 '14 at 05:04PartinAssociation, not elegant. What do you think? – matheorem Dec 13 '14 at 05:04Span. It returns the selected parts wrapped in their original head. So you only get aListback if you are extracting parts from aList. For examplef[1, 2, 3][[1;;2]]givesf[1, 2]not{1, 2}. – Simon Woods Dec 13 '14 at 14:32Association. Since assoc[[1]] directly acts on values of assoc, then why doesn't assoc[[span]] gives collection of values? I admit I made a mistake. Like you said, original head should be preserved. Then,<|"a" -> 1, "b" -> 2, "c" -> 3, "d" -> 4|>[[1;;2]]should give<|1,2|>, but this is not an association!It is a contradiction! So they must make it as<|"a" -> 1, "b" -> 2|>. Wow, maybe this is what the creators ofAssociationthought in their mind!!! You enlightened me!:) What do you think? – matheorem Dec 13 '14 at 14:48