Skip to content

Commit 94f0509

Browse files
authored
unexport lag and lead (#55)
* unexport lag and lead * updated doctests * update docs and remove unneeded section * doc fixes
1 parent 2c64bd7 commit 94f0509

File tree

6 files changed

+65
-64
lines changed

6 files changed

+65
-64
lines changed

NEWS.md

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
1+
## ShiftedArrays 2.0.0 release notes
2+
3+
### Breaking changes
4+
5+
- Indexing out of bounds now gives a `BoundsError` (instead of returning the default value for `ShiftedArray` or the circularly shifted value for `CircShiftedArray`).
6+
- `lag` and `lead` are no longer exported (but still public API).
7+
- Calling `ShiftedArray(v::ShiftedArray, n)` or `CircShiftedArray(v::CircShiftedArray, n)` does not nest but rather combines the shifts additively.
8+
19
## ShiftedArrays 1.0.0 release notes
210

311
### Breaking changes
412

5-
- Removed special `reduce, reduce_vec, mapreduce, mapreduce_vec` methods
6-
- Removed `to_array, to_offsetarray` methods
13+
- Removed special `reduce, reduce_vec, mapreduce, mapreduce_vec` methods.
14+
- Removed `to_array, to_offsetarray` methods.
715

816
## ShiftedArrays 0.5.1 release notes
917

1018
### New features
1119

12-
- Support for OffsetArrays v0.11
13-
- Support for RecursiveArrayTools v1
20+
- Support for OffsetArrays v0.11.
21+
- Support for RecursiveArrayTools v1.
1422

1523
## ShiftedArrays 0.5.0 release notes
1624

1725
### New features
1826

19-
- Support for Julia 1
27+
- Support for Julia 1.
2028

2129
## ShiftedArrays 0.4 release notes
2230

@@ -29,8 +37,8 @@
2937

3038
### New features
3139

32-
- Allow custom default value with `default` keyword
33-
- Allow filtering in reduce-like functions with `filter` keyword
40+
- Allow custom default value with `default` keyword.
41+
- Allow filtering in reduce-like functions with `filter` keyword.
3442

3543
## ShiftedArrays 0.3 release notes
3644

@@ -42,4 +50,4 @@
4250
### New features
4351

4452
- `CircShiftedArray` type to shift arrays circularly.
45-
- A lazy version of `circshift`: `ShiftedArray.circshift`
53+
- A lazy version of `circshift`: `ShiftedArray.circshift`.

docs/src/api.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ CircShiftedVector
1212
## Shifting operations
1313

1414
```@docs
15-
lag
16-
lead
15+
ShiftedArrays.lag
16+
ShiftedArrays.lead
1717
ShiftedArrays.circshift
1818
```
1919

docs/src/index.md

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,25 @@ A `ShiftedArray` is a lazy view of an Array, shifted on some or all of his index
88

99
```julia
1010
julia> v = reshape(1:16, 4, 4)
11-
4×4 Base.ReshapedArray{Int64,2,UnitRange{Int64},Tuple{}}:
11+
4×4 reshape(::UnitRange{Int64}, 4, 4) with eltype Int64:
1212
1 5 9 13
1313
2 6 10 14
1414
3 7 11 15
1515
4 8 12 16
1616

17-
julia> s = ShiftedArray(v, (2, 0))
18-
4×4 ShiftedArrays.ShiftedArray{Int64,2,Base.ReshapedArray{Int64,2,UnitRange{Int64},Tuple{}}}:
19-
missing missing missing missing
20-
missing missing missing missing
21-
1 5 9 13
22-
2 6 10 14
17+
julia> s = ShiftedArray(v, (2, 0))
18+
4×4 ShiftedArray{Int64, Missing, 2, Base.ReshapedArray{Int64, 2, UnitRange{Int64}, Tuple{}}}:
19+
missing missing missing missing
20+
missing missing missing missing
21+
1 5 9 13
22+
2 6 10 14
2323
```
2424

2525
The parent Array as well as the amount of shifting can be recovered with `parent` and `shifts` respectively.
2626

2727
```julia
2828
julia> parent(s)
29-
4×4 Base.ReshapedArray{Int64,2,UnitRange{Int64},Tuple{}}:
29+
4×4 reshape(::UnitRange{Int64}, 4, 4) with eltype Int64:
3030
1 5 9 13
3131
2 6 10 14
3232
3 7 11 15
@@ -42,18 +42,18 @@ Use `copy` to collect the shifted data into an `Array`:
4242

4343
```julia
4444
julia> copy(s)
45-
4×4 Array{Union{Int64, Missing},2}:
45+
4×4 Matrix{Union{Missing, Int64}}:
4646
missing missing missing missing
4747
missing missing missing missing
48-
1 5 9 13
48+
1 5 9 13
4949
2 6 10 14
5050
```
5151

5252
If you pass an integer, it will shift in the first dimension:
5353

5454
```julia
5555
julia> ShiftedArray(v, 1)
56-
4×4 ShiftedArrays.ShiftedArray{Int64,2,Base.ReshapedArray{Int64,2,UnitRange{Int64},Tuple{}}}:
56+
4×4 ShiftedArray{Int64, Missing, 2, Base.ReshapedArray{Int64, 2, UnitRange{Int64}, Tuple{}}}:
5757
missing missing missing missing
5858
1 5 9 13
5959
2 6 10 14
@@ -64,25 +64,19 @@ A custom default value (other than `missing`) can be provided with the `default`
6464

6565
```julia
6666
julia> ShiftedArray([1.2, 3.1, 4.5], 1, default = NaN)
67-
3-element ShiftedArrays.ShiftedArray{Float64,Float64,1,Array{Float64,1}}:
67+
3-element ShiftedVector{Float64, Float64, Vector{Float64}}:
6868
NaN
6969
1.2
7070
3.1
7171
```
7272

7373
### Out of bound indexes
7474

75-
The bound check is performed only on the parent `Array`, not on the `ShiftedArray`, so for example:
75+
Accessing indexes outside the `ShiftedArray` give a `BoundsError`, even if the shifted index would have been valid in the parent array.
7676

7777
```julia
78-
julia> ShiftedArray([1.2, 3.1, 4.5], 1, default = NaN)[-2:3]
79-
6-element Array{Float64,1}:
80-
NaN
81-
NaN
82-
NaN
83-
NaN
84-
1.2
85-
3.1
78+
julia> ShiftedArray([1, 2, 3], 1)[4]
79+
ERROR: BoundsError: attempt to access 3-element ShiftedVector{Int64, Missing, Vector{Int64}} at index [4]
8680
```
8781

8882
## Shifting the data
@@ -92,25 +86,25 @@ Using the `ShiftedArray` type, this package provides two operations for lazily s
9286
```julia
9387
julia> v = [1, 3, 5, 4];
9488

95-
julia> lag(v)
96-
4-element ShiftedArrays.ShiftedArray{Int64,1,Array{Int64,1}}:
89+
julia> ShiftedArrays.lag(v)
90+
4-element ShiftedVector{Int64, Missing, Vector{Int64}}:
9791
missing
98-
1
99-
3
92+
1
93+
3
10094
5
10195

102-
julia> v .- lag(v) # compute difference from previous element without unnecessary allocations
103-
4-element Array{Any,1}:
96+
julia> v .- ShiftedArrays.lag(v) # compute difference from previous element without unnecessary allocations
97+
4-element Vector{Union{Missing, Int64}}:
10498
missing
105-
2
106-
2
99+
2
100+
2
107101
-1
108102

109-
julia> s = lag(v, 2) # shift by more than one element
110-
4-element ShiftedArrays.ShiftedArray{Int64,1,Array{Int64,1}}:
103+
julia> s = ShiftedArrays.lag(v, 2) # shift by more than one element
104+
4-element ShiftedVector{Int64, Missing, Vector{Int64}}:
111105
missing
112106
missing
113-
1
107+
1
114108
3
115109
```
116110

@@ -119,11 +113,11 @@ julia> s = lag(v, 2) # shift by more than one element
119113
```julia
120114
julia> v = [1, 3, 5, 4];
121115

122-
julia> lead(v)
123-
4-element ShiftedArrays.ShiftedArray{Int64,1,Array{Int64,1}}:
124-
3
125-
5
126-
4
116+
julia> ShiftedArrays.lead(v)
117+
4-element ShiftedVector{Int64, Missing, Vector{Int64}}:
118+
3
119+
5
120+
4
127121
missing
128122
```
129123

@@ -140,7 +134,7 @@ Our implementation of `circshift` relies on them to avoid copying:
140134
julia> w = reshape(1:16, 4, 4);
141135

142136
julia> s = ShiftedArrays.circshift(w, (1, -1))
143-
4×4 ShiftedArrays.CircShiftedArray{Int64,2,Base.ReshapedArray{Int64,2,UnitRange{Int64},Tuple{}}}:
137+
4×4 CircShiftedArray{Int64, 2, Base.ReshapedArray{Int64, 2, UnitRange{Int64}, Tuple{}}}:
144138
8 12 16 4
145139
5 9 13 1
146140
6 10 14 2
@@ -151,7 +145,7 @@ As usual, you can `copy` the result to have a normal `Array`:
151145

152146
```julia
153147
julia> copy(s)
154-
4×4 Array{Int64,2}:
148+
4×4 Matrix{Int64}:
155149
8 12 16 4
156150
5 9 13 1
157151
6 10 14 2

src/ShiftedArrays.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module ShiftedArrays
33
import Base: checkbounds, getindex, setindex!, parent, size, axes
44
export ShiftedArray, ShiftedVector, shifts, default
55
export CircShiftedArray, CircShiftedVector
6-
export lag, lead
76

87
include("shiftedarray.jl")
98
include("circshiftedarray.jl")

src/lag.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ remaining dimensions is assumed to be `0`.
1212
```jldoctest lag
1313
julia> v = [1, 3, 5, 4];
1414
15-
julia> lag(v)
15+
julia> ShiftedArrays.lag(v)
1616
4-element ShiftedVector{Int64, Missing, Vector{Int64}}:
1717
missing
1818
1
@@ -22,7 +22,7 @@ julia> lag(v)
2222
julia> w = 1:2:9
2323
1:2:9
2424
25-
julia> s = lag(w, 2)
25+
julia> s = ShiftedArrays.lag(w, 2)
2626
5-element ShiftedVector{Int64, Missing, StepRange{Int64, Int64}}:
2727
missing
2828
missing
@@ -40,7 +40,7 @@ julia> copy(s)
4040
4141
julia> v = reshape(1:16, 4, 4);
4242
43-
julia> s = lag(v, (0, 2))
43+
julia> s = ShiftedArrays.lag(v, (0, 2))
4444
4×4 ShiftedArray{Int64, Missing, 2, Base.ReshapedArray{Int64, 2, UnitRange{Int64}, Tuple{}}}:
4545
missing missing 1 5
4646
missing missing 2 6
@@ -66,7 +66,7 @@ remaining dimensions is assumed to be `0`.
6666
```jldoctest lead
6767
julia> v = [1, 3, 5, 4];
6868
69-
julia> lead(v)
69+
julia> ShiftedArrays.lead(v)
7070
4-element ShiftedVector{Int64, Missing, Vector{Int64}}:
7171
3
7272
5
@@ -76,7 +76,7 @@ julia> lead(v)
7676
julia> w = 1:2:9
7777
1:2:9
7878
79-
julia> s = lead(w, 2)
79+
julia> s = ShiftedArrays.lead(w, 2)
8080
5-element ShiftedVector{Int64, Missing, StepRange{Int64, Int64}}:
8181
5
8282
7
@@ -94,7 +94,7 @@ julia> copy(s)
9494
9595
julia> v = reshape(1:16, 4, 4);
9696
97-
julia> s = lead(v, (0, 2))
97+
julia> s = ShiftedArrays.lead(v, (0, 2))
9898
4×4 ShiftedArray{Int64, Missing, 2, Base.ReshapedArray{Int64, 2, UnitRange{Int64}, Tuple{}}}:
9999
9 13 missing missing
100100
10 14 missing missing

test/runtests.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,22 +163,22 @@ end
163163

164164
@testset "laglead" begin
165165
v = [1, 3, 8, 12]
166-
diff = v .- lag(v)
166+
diff = v .- ShiftedArrays.lag(v)
167167
@test isequal(diff, [missing, 2, 5, 4])
168168

169-
diff2 = v .- lag(v, 2)
169+
diff2 = v .- ShiftedArrays.lag(v, 2)
170170
@test isequal(diff2, [missing, missing, 7, 9])
171171

172-
@test all(lag(v, 2, default = -100) .== coalesce.(lag(v, 2), -100))
172+
@test all(ShiftedArrays.lag(v, 2, default = -100) .== coalesce.(ShiftedArrays.lag(v, 2), -100))
173173

174-
diff = v .- lead(v)
174+
diff = v .- ShiftedArrays.lead(v)
175175
@test isequal(diff, [-2, -5, -4, missing])
176176

177-
diff2 = v .- lead(v, 2)
177+
diff2 = v .- ShiftedArrays.lead(v, 2)
178178
@test isequal(diff2, [-7, -9, missing, missing])
179179

180-
@test all(lead(v, 2, default = -100) .== coalesce.(lead(v, 2), -100))
180+
@test all(ShiftedArrays.lead(v, 2, default = -100) .== coalesce.(ShiftedArrays.lead(v, 2), -100))
181181

182-
@test lag(lag(v, 1), 2) === lag(v, 3)
183-
@test lead(lead(v, 1), 2) === lead(v, 3)
182+
@test ShiftedArrays.lag(ShiftedArrays.lag(v, 1), 2) === ShiftedArrays.lag(v, 3)
183+
@test ShiftedArrays.lead(ShiftedArrays.lead(v, 1), 2) === ShiftedArrays.lead(v, 3)
184184
end

0 commit comments

Comments
 (0)