only

Assemble values into a range that carries all its elements in-situ.

Useful when a single value or multiple disconnected values must be passed to an algorithm expecting a range, without having to perform dynamic memory allocation.

As copying the range means copying all elements, it can be safely returned from functions. For the same reason, copying the returned range may be expensive for a large number of arguments.

only
(
Values...
)
(
return scope Values values
)
if (
!is(CommonType!Values == void) ||
Values.length == 0
)

Parameters

values Values

the values to assemble together

Return Value

Type: auto

A RandomAccessRange of the assembled values.

Examples

1 import std.algorithm.comparison : equal;
2 import std.algorithm.iteration : filter, joiner, map;
3 import std.algorithm.searching : findSplitBefore;
4 import std.uni : isUpper;
5 
6 assert(equal(only('♡'), "♡"));
7 assert([1, 2, 3, 4].findSplitBefore(only(3))[0] == [1, 2]);
8 
9 assert(only("one", "two", "three").joiner(" ").equal("one two three"));
10 
11 string title = "The D Programming Language";
12 assert(title
13     .filter!isUpper // take the upper case letters
14     .map!only       // make each letter its own range
15     .joiner(".")    // join the ranges together lazily
16     .equal("T.D.P.L"));

See Also

chain to chain ranges

Meta

Suggestion Box / Bug Report