Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add opDollar aliases for std.range and std.algorithm. #365

Merged
merged 1 commit into from

3 participants

@dsimcha
Collaborator

No description provided.

@andralex
Owner

This is exactly the kind of boilerplate that we'd like to avoid. Let me talk to Walter about fixing that in a different way.

@dsimcha
Collaborator
@MartinNowak
Collaborator

Please decide what to do with this pull request.
Consider filing an enhancement request and closing it.

@andralex
Owner

I'll discuss it with Walter.

@dsimcha
Collaborator

So we're suggesting that UFCS include operators, not just named methods? Interesting.

@andralex
Owner

I think I'll merge this in and defer looking into a simpler solution.

@andralex andralex merged commit 91c4363 into from
@schveiguy schveiguy referenced this pull request in schveiguy/phobos
Merged

std.process ready for review? #1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 11, 2011
  1. @dsimcha
This page is out of date. Refresh to see the latest.
Showing with 53 additions and 0 deletions.
  1. +7 −0 std/algorithm.d
  2. +46 −0 std/range.d
View
7 std/algorithm.d
@@ -437,6 +437,8 @@ template map(fun...) if (fun.length >= 1)
{
return _input.length;
}
+
+ alias length opDollar;
}
static if (hasSlicing!R)
@@ -483,6 +485,7 @@ unittest
const int[] arr1Const = arr1;
int[] arr2 = [ 5, 6 ];
auto squares = map!("a * a")(arr1Const);
+ assert(squares[$ - 1] == 16);
assert(equal(squares, [ 1, 4, 9, 16 ][]));
assert(equal(map!("a * a")(chain(arr1, arr2)), [ 1, 4, 9, 16, 25, 36 ][]));
@@ -3264,6 +3267,8 @@ public:
{
return needle.length;
}
+
+ alias length opDollar;
}
/// Ditto
@@ -7928,6 +7933,8 @@ public:
}
return result;
}
+
+ alias length opDollar;
}
}
View
46 std/range.d
@@ -142,6 +142,8 @@ enum dummyRanges = q{
@property size_t length() {
return arr.length;
}
+
+ alias length opDollar;
}
}
@@ -600,6 +602,7 @@ unittest
void popBack();
ref int opIndex(uint);
@property size_t length();
+ alias length opDollar;
//int opSlice(uint, uint);
}
static assert(isRandomAccessRange!(D));
@@ -626,6 +629,7 @@ unittest
int opIndex(size_t n) const { return 0; }
@property size_t length() const { return 0; }
+ alias length opDollar;
void put(int e){ }
}
@@ -1066,6 +1070,8 @@ if (isBidirectionalRange!(Unqual!Range))
{
return source.length;
}
+
+ alias length opDollar;
}
}
@@ -1360,10 +1366,14 @@ if (isInputRange!(Unqual!Range))
}
static if (hasLength!R)
+ {
@property auto length()
{
return (source.length + _n - 1) / _n;
}
+
+ alias length opDollar;
+ }
}
return Result(r, n);
}
@@ -1706,6 +1716,7 @@ if (Ranges.length > 0 && allSatisfy!(isInputRange, staticMap!(Unqual, Ranges)))
}
static if (allSatisfy!(hasLength, R))
+ {
@property size_t length()
{
size_t result;
@@ -1715,6 +1726,9 @@ if (Ranges.length > 0 && allSatisfy!(isInputRange, staticMap!(Unqual, Ranges)))
}
return result;
}
+
+ alias length opDollar;
+ }
static if (allSatisfy!(isRandomAccessRange, R))
{
@@ -2024,6 +2038,8 @@ if (Rs.length > 1 && allSatisfy!(isInputRange, staticMap!(Unqual, Rs)))
}
return result;
}
+
+ alias length opDollar;
}
}
@@ -2185,6 +2201,8 @@ if (isInputRange!(Unqual!Range)
{
return _maxAvailable;
}
+
+ alias length opDollar;
}
else static if (hasLength!R)
{
@@ -2192,6 +2210,8 @@ if (isInputRange!(Unqual!Range)
{
return min(_maxAvailable, source.length);
}
+
+ alias length opDollar;
}
static if (isRandomAccessRange!R)
@@ -2412,6 +2432,7 @@ if (isInputRange!R && !hasSlicing!R)
}
void popFront() { _input.popFront(); --_n; }
@property size_t length() const { return _n; }
+ alias length opDollar;
static if (isForwardRange!R)
auto save() { return this; }
@@ -2505,6 +2526,7 @@ auto takeOne(R)(R source) if (isInputRange!R)
auto save() { return Result(_source.save, empty); }
@property auto ref back() { assert(!empty); return _source.front; }
@property size_t length() const { return !empty; }
+ alias length opDollar;
auto ref opIndex(size_t n) { assert(n < length); return _source.front; }
auto opSlice(size_t m, size_t n)
{
@@ -3257,6 +3279,7 @@ if(Ranges.length && allSatisfy!(isInputRange, staticMap!(Unqual, Ranges)))
$(D length).
*/
static if (allSatisfy!(hasLength, R))
+ {
@property auto length()
{
CommonType!(staticMap!(lengthType, R)) result = ranges[0].length;
@@ -3276,6 +3299,9 @@ if(Ranges.length && allSatisfy!(isInputRange, staticMap!(Unqual, Ranges)))
}
return result;
}
+
+ alias length opDollar;
+ }
/**
Returns a slice of the range. Defined only if all range define
@@ -4056,6 +4082,8 @@ if ((isIntegral!(CommonType!(B, E)) || isPointer!(CommonType!(B, E)))
{
return unsigned((pastLast - current) / step);
}
+
+ alias length opDollar;
}
return Result(begin, end, step);
@@ -4120,6 +4148,8 @@ if (isIntegral!(CommonType!(B, E)) || isPointer!(CommonType!(B, E)))
{
return unsigned(pastLast - current);
}
+
+ alias length opDollar;
}
return Result(begin, end);
@@ -4203,6 +4233,8 @@ if (isFloatingPoint!(CommonType!(B, E, S)))
{
return count - index;
}
+
+ alias length opDollar;
}
return Result(begin, end, step);
@@ -4212,6 +4244,7 @@ unittest
{
static assert(hasLength!(typeof(iota(0, 2))));
auto r = iota(0, 10, 1);
+ assert(r[$ - 1] == 9);
assert(equal(r, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9][]));
auto rSlice = r[2..8];
@@ -4806,6 +4839,8 @@ struct Transversal(Ror,
{
return _input.length;
}
+
+ alias length opDollar;
}
/**
@@ -5096,6 +5131,8 @@ if(isRandomAccessRange!Source && isInputRange!Indices &&
{
return _indices.length;
}
+
+ alias length opDollar;
}
static if(isRandomAccessRange!Indices)
@@ -5290,6 +5327,8 @@ struct Chunks(Source) if(hasSlicing!Source && hasLength!Source)
return (_source.length / _chunkSize) +
(_source.length % _chunkSize > 0);
}
+
+ alias length opDollar;
/// Ditto
@property auto back()
@@ -5582,6 +5621,9 @@ interface RandomAccessFinite(E) : BidirectionalRange!(E) {
///
@property size_t length();
+
+ ///
+ alias length opDollar;
// Can't support slicing until issues with requiring slicing for all
// finite random access ranges are fully resolved.
@@ -5781,6 +5823,8 @@ template InputRangeObject(R) if (isInputRange!(Unqual!R)) {
@property size_t length() {
return _range.length;
}
+
+ alias length opDollar;
// Can't support slicing until all the issues with
// requiring slicing support for finite random access
@@ -6086,6 +6130,8 @@ if (isRandomAccessRange!Range)
{
return _input.length;
}
+
+ alias length opDollar;
/**
Releases the controlled range and returns it.
Something went wrong with that request. Please try again.