1 /// Drops elements from a range
2 module ddash.range.drop;
3 
4 ///
5 unittest {
6     assert([1, 2, 3, 4].drop.array == [2, 3, 4]);
7     assert([1, 2, 3, 4].dropRight.array == [1, 2, 3]);
8     assert([1, 2, 3, 4].dropWhile!(a => a < 3).array == [3, 4]);
9     assert([1, 2, 3, 4].dropRightWhile!(a => a > 2).equal([1, 2]));
10 
11 }
12 
13 import ddash.common;
14 
15 
16 /**
17     Drops n elements from beginning of range
18 
19     Params:
20         range = input range
21         n = number of elements to drop
22 
23     Returns:
24         new range
25 
26     Since:
27         0.0.1
28 */
29 auto drop(Range)(Range range, size_t n = 1) if (from!"std.range".isInputRange!Range) {
30     import std.range: stdDrop = drop;
31     return range.stdDrop(n);
32 }
33 
34 ///
35 unittest {
36     import std.array;
37     assert([1, 2, 3].drop.equal([2, 3]));
38 }
39 
40 /**
41     Drops n elements from end of range
42 
43     Params:
44         range = input range
45         n = number of elements to drop
46 
47     Returns:
48         new range
49 
50     Since:
51         0.0.1
52 */
53 auto dropRight(Range)(Range range, size_t n = 1) if (from!"std.range".isBidirectionalRange!Range) {
54     import std.range: stdDropBack = dropBack;
55     return range.stdDropBack(n);
56 }
57 
58 ///
59 unittest {
60     import std.array;
61     assert([1, 2, 3].dropRight.equal([1, 2]));
62 }
63 
64 /**
65     Drops elements from beginnig of range while predicate is true
66 
67     Params:
68         pred = comparator
69         range = input range
70 
71     Returns:
72         new range
73 
74     Since:
75         0.0.1
76 */
77 auto dropWhile(alias pred, Range)(Range range) if (from!"std.range".isInputRange!Range) {
78     import std.functional: unaryFun;
79     import std.range: empty, back, popBack;
80     while (!range.empty && unaryFun!pred(range.front)) range.popFront;
81     return range;
82 }
83 
84 ///
85 unittest {
86     import std.array;
87     assert([1, 2, 3, 4].dropWhile!(a => a < 3).equal([3, 4]));
88 }
89 
90 /**
91     Drops elements from end of range while predicate is true
92 
93     Params:
94         pred = comparator
95         range = input range
96 
97     Returns:
98         new range
99 
100     Since:
101         0.0.1
102 */
103 auto dropRightWhile(alias pred, Range)(Range range) if (from!"std.range".isBidirectionalRange!Range) {
104     import std.functional: unaryFun;
105     import std.range: empty, back, popBack;
106     while (!range.empty && unaryFun!pred(range.back)) range.popBack;
107     return range;
108 }