Skip to content

Commit 7dad7c2

Browse files
authored
[std/array] Improve assocArray docs (dlang#10908)
* [std/array] Improve `assocArray` docs Split docs for overloads, rearrange/improve wording. Add nothrow to unittests. Add example showing how to duplicate an AA. Remove redundant `zip` now there's a separate overload. Remove `byPair` example because we have `aa.dup` now
1 parent c47daab commit 7dad7c2

File tree

1 file changed

+39
-29
lines changed

1 file changed

+39
-29
lines changed

std/array.d

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ $(TR $(TH Function Name) $(TH Description)
1616
$(TD Returns a new $(LREF Appender) or $(LREF RefAppender) initialized with a given array.
1717
))
1818
$(TR $(TD $(LREF assocArray))
19-
$(TD Returns a newly allocated associative array from a range of key/value tuples.
19+
$(TD Returns a newly allocated associative array from a range/ranges of keys and values.
2020
))
2121
$(TR $(TD $(LREF byPair))
2222
$(TD Construct a range iterating over an associative array by key/value tuples.
@@ -562,29 +562,19 @@ if (isAutodecodableString!String)
562562
}
563563

564564
/**
565-
Returns a newly allocated associative array from a range of key/value tuples
566-
or from a range of keys and a range of values.
565+
Creates an associative array from a range of key/value tuples.
567566
568567
Params:
569568
r = An $(REF_ALTTEXT input range, isInputRange, std,range,primitives)
570-
of tuples of keys and values.
571-
keys = An $(REF_ALTTEXT input range, isInputRange, std,range,primitives) of keys
572-
values = An $(REF_ALTTEXT input range, isInputRange, std,range,primitives) of values
569+
of $(REF_SHORT Tuple, std,typecons)`!(Key, Value)`.
573570
574-
Returns:
571+
Duplicates: Associative arrays have unique keys. For any duplicate key in `r`,
572+
the result will contain the corresponding value from the last occurrence of that key in `r`.
575573
576-
A newly allocated associative array out of elements of the input
577-
range, which must be a range of tuples (Key, Value) or
578-
a range of keys and a range of values. If given two ranges of unequal
579-
lengths after the elements of the shorter are exhausted the remaining
580-
elements of the longer will not be considered.
581-
Returns a null associative array reference when given an empty range.
582-
Duplicates: Associative arrays have unique keys. If r contains duplicate keys,
583-
then the result will contain the value of the last pair for that key in r.
584-
585-
See_Also: $(REF Tuple, std,typecons), $(REF zip, std,range)
574+
Returns:
575+
A newly allocated associative array, or a null associative array reference when
576+
given an empty range. The type is `Value[Key]`.
586577
*/
587-
588578
auto assocArray(Range)(Range r)
589579
if (isInputRange!Range)
590580
{
@@ -604,7 +594,34 @@ if (isInputRange!Range)
604594
return aa;
605595
}
606596

607-
/// ditto
597+
///
598+
@safe pure nothrow unittest
599+
{
600+
import std.typecons : tuple;
601+
602+
auto b = assocArray([ tuple("foo", "bar"), tuple("baz", "quux") ]);
603+
static assert(is(typeof(b) == string[string]));
604+
assert(b == ["foo":"bar", "baz":"quux"]);
605+
}
606+
607+
/**
608+
Creates an associative array from a range of keys and a range of values.
609+
610+
If given two ranges of unequal lengths after the elements of the shorter are exhausted,
611+
the remaining elements of the longer will not be considered.
612+
613+
Params:
614+
keys = An $(REF_ALTTEXT input range, isInputRange, std,range,primitives) of keys
615+
values = An $(REF_ALTTEXT input range, isInputRange, std,range,primitives) of values
616+
617+
Duplicates: Associative arrays have unique keys. For any key with duplicates in `keys`,
618+
the result will have the corresponding value for the last occurrence of
619+
that key in `keys`.
620+
621+
Returns:
622+
A newly allocated associative array, or a null associative array reference when
623+
given empty ranges.
624+
*/
608625
auto assocArray(Keys, Values)(Keys keys, Values values)
609626
if (isInputRange!Values && isInputRange!Keys)
610627
{
@@ -682,23 +699,16 @@ if (isInputRange!Values && isInputRange!Keys)
682699
}
683700

684701
///
685-
@safe pure /*nothrow*/ unittest
702+
@safe pure nothrow unittest
686703
{
687-
import std.range : repeat, zip;
688-
import std.typecons : tuple;
689704
import std.range.primitives : autodecodeStrings;
690-
auto a = assocArray(zip([0, 1, 2], ["a", "b", "c"])); // aka zipMap
691-
static assert(is(typeof(a) == string[int]));
692-
assert(a == [0:"a", 1:"b", 2:"c"]);
693-
694-
auto b = assocArray([ tuple("foo", "bar"), tuple("baz", "quux") ]);
695-
static assert(is(typeof(b) == string[string]));
696-
assert(b == ["foo":"bar", "baz":"quux"]);
705+
import std.range : repeat;
697706

698707
static if (autodecodeStrings)
699708
alias achar = dchar;
700709
else
701710
alias achar = immutable(char);
711+
702712
auto c = assocArray("ABCD", true.repeat);
703713
static assert(is(typeof(c) == bool[achar]));
704714
bool[achar] expected = ['D':true, 'A':true, 'B':true, 'C':true];

0 commit comments

Comments
 (0)