dranges.phobos_extension
This module provides modified versions of std.algorithm and std.range functions.
If you're interested, they should be put in your local copy of std.
The most simple/solid ones are map and filter. Chain and take are much more 'fragile'.
License:
Boost License 1.0.
Authors:
Philippe Sigaud
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
- struct
Map
(alias fun,Range) if (isInputRange!(Range));
- An extended version of std.algorithm.
Map
, that makes it propagate the
input range properties (bidirectional, etc.)
- it defines back/popBack if the input range is bidirectional.
- it defines opIndex if the range offers random-access.
- it has a length if the input range has one.
- it's infinite if the input range is infinite.
- it can be sliced if the input range can also.
Examples:
auto r = [0,1,2,3,4]; // random-access range with a length and slicing.
auto m = map!"a*a"(r);
assert(equal(retro(m), [16,9,4,1,0][])); // bidirectional range
assert(m[3] == 9); // random-access range
assert(m.length == 5); // length
assert(equal(m[1..3], [1,4][])); // slicing
auto m2 = map!"a*a"(cycle(r)); // cycle(r) is infinite
assert(isInfinite(typeof(m2))); // m2 is infinite also.
assert(!is(m2.length)); // cycle(r) doesn't have a length, so neither has m2.
Note:
map caches its front and back. So be prudent if you use callable classes/structs
with an internal state (and why would you use a class as a callable if not to manage
state?).
- struct
Filter
(alias pred,Range) if (isInputRange!(Range));
- An extended version of std.algorithm.
Filter
that defines
back/popBack if the input range is bidirectional.
Example:
auto r = [0,1,2,3,4];
auto f = filter!"a%2==0"(r);
assert(equal(retro(f), [4,2,0])); // f is a bidirectional range
- struct
Repeat
(T);
- std.range.
Repeat
should have popBack defined.
- struct
Stride
(R) if (isInputRange!(R));
- std.range.
Stride
assumes a bidirectional range. This slight modification
just add the necessary 'isBidirectionalRange!R' throughout the code.
- struct
Take
(R) if (isInputRange!(R));
Take!(R)
take
(R)(size_t n, R input);
- A corrected version of std.range.take.
/+It uses slicing when possible, to get (hopefully) a better performance.+/
The std version has problems with its back/popBack methods, they simply do not work.
It seems these do work (to be thoroughly tested, though), but they depend on rather
strict conditions on the input range: back/popBack needs a random-access range
with a length.
- struct
ChainImpl
(R...);
- An extended version of std.range.
ChainImpl
.
In DMD 2.037, chain has some problems:
- opIndex doesn't deal correctly with infinite ranges
- the same for opIndexAssign
auto c = chain([0,1,2][], cycle([4,5,6][]), [7,8,9][]); // infinite range inside.
auto c7 = c[7]; // doesn't work with std.range.chain.
This version also provides some concatenation capabilities on the right
with another chain (with flattening), with another range and with an element.
|