Repository: invenia/NamedDims.jl
Pull request #68 (2019-10-02 15:21:47)

Mutating functions
This should make `push!` and friends work.


Patch:
diff --git a/Project.toml b/Project.toml
index 5ccafad..09be274 100644
--- a/Project.toml
+++ b/Project.toml
@@ -1,7 +1,7 @@
 name = "NamedDims"
 uuid = "356022a1-0364-5f58-8944-0da4b18d706f"
 authors = ["Invenia Technical Computing Corporation"]
-version = "0.2.4"
+version = "0.2.5"
 
 [deps]
 LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
diff --git a/README.md b/README.md
index 9e80ebc..514ee6b 100644
--- a/README.md
+++ b/README.md
@@ -7,7 +7,6 @@
 [![Build Status](https://travis-ci.com/invenia/NamedDims.jl.svg?branch=master)](https://travis-ci.com/invenia/NamedDims.jl)
 [![Build Status](https://ci.appveyor.com/api/projects/status/github/invenia/NamedDims.jl?svg=true)](https://ci.appveyor.com/project/invenia/NamedDims-jl)
 [![Codecov](https://codecov.io/gh/invenia/NamedDims.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/invenia/NamedDims.jl)
-[![code style blue](https://img.shields.io/badge/code%20style-blue-4495d1.svg)](https://github.com/invenia/BlueStyle)
 
 `NamedDimsArray` is a zero-cost abstraction to add names to the dimensions of an array.
 
diff --git a/src/functions.jl b/src/functions.jl
index fb89623..48d31eb 100644
--- a/src/functions.jl
+++ b/src/functions.jl
@@ -90,14 +90,23 @@ end
 ################################################
 # Non-dim Overloads
 
-# 1 Arg
+# Array then perhaps other args
 for (mod, funs) in (
-    (:Base, (:zero, :one, :copy,)),
+    (:Base, (:zero, :one, :copy, :empty!, :push!, :pushfirst!)),
 )
     for fun in funs
-        @eval function $mod.$fun(a::NamedDimsArray{L}) where L
-            data = $mod.$fun(parent(a))
+        @eval function $mod.$fun(a::NamedDimsArray{L}, x...) where L
+            data = $mod.$fun(parent(a), x...)
             return NamedDimsArray{L}(data)
         end
     end
 end
+
+Base.pop!(A::NamedDimsArray) = pop!(parent(A))
+Base.popfirst!(A::NamedDimsArray) = popfirst!(parent(A))
+
+function Base.append!(A::NamedDimsArray{L,T,1}, B::AbstractVector) where {L,T}
+    newL = unify_names(L, names(B))
+    data = append!(parent(A), unname(B))
+    return NamedDimsArray{newL}(data)
+end
diff --git a/src/wrapper_array.jl b/src/wrapper_array.jl
index 1c1e23a..cef1ac6 100644
--- a/src/wrapper_array.jl
+++ b/src/wrapper_array.jl
@@ -35,7 +35,6 @@ struct NamedDimsArray{L, T, N, A<:AbstractArray{T, N}} <: AbstractArray{T, N}
     data::A
 end
 
-
 function NamedDimsArray{L}(orig::AbstractArray{T, N}) where {L, T, N}
     if !(L isa NTuple{N, Symbol})
         throw(ArgumentError(
@@ -57,8 +56,8 @@ function NamedDimsArray{L}(orig::NamedDimsArray{old_names, T, N, A}) where {L, o
     return NamedDimsArray{new_names, T, N, A}(parent(orig))
 end
 
-
 parent_type(::Type{<:NamedDimsArray{L, T, N, A}}) where {L, T, N, A} = A
+
 Base.parent(x::NamedDimsArray) = x.data
 
 """
@@ -126,7 +125,6 @@ function Base.similar(
     return similar(a, eltype, dims)
 end
 
-
 function Base.similar(
     a::NamedDimsArray{L, T, N},
     eltype::Type,


Test patch:
diff --git a/test/functions.jl b/test/functions.jl
index 9304aa5..fdc77ca 100644
--- a/test/functions.jl
+++ b/test/functions.jl
@@ -137,6 +137,34 @@ using Statistics
         @test_throws ErrorException count(nda; dims=:x)
         @test_throws ErrorException count(a; dims=1)
     end
+
+    @testset "push!, pop!, etc" begin
+        ndv = NamedDimsArray([10, 20, 30], (:i,))
+
+        @test length(push!(ndv, 40)) == 4
+        @test names(pushfirst!(ndv, 0)) == (:i,)
+        @test ndv == 0:10:40
+
+        @test pop!(ndv) == 40
+        @test popfirst!(ndv) == 0
+        @test ndv == [10, 20, 30]
+    end
+
+    @testset "append!, empty!" begin
+        ndv = NamedDimsArray([10, 20, 30], (:i,))
+        ndv45 = NamedDimsArray([40, 50], (:i,))
+        ndv0 = NamedDimsArray([0, 0], (:zero,))
+
+        @test length(append!(ndv, ndv45)) == 5
+        @test names(append!(ndv, [60,70])) == (:i,)
+
+        @test_throws DimensionMismatch append!(ndv, ndv0)
+        @test ndv == 10:10:70 # error was thrown before altering
+
+        @test names(empty!(ndv)) == (:i,)
+        @test length(ndv) == 0
+end
+
 end  # Base
 
 @testset "Statistics" begin
diff --git a/test/wrapper_array.jl b/test/wrapper_array.jl
index 4d9c9e7..5712981 100644
--- a/test/wrapper_array.jl
+++ b/test/wrapper_array.jl
@@ -149,3 +149,4 @@ end
         @test names(ndb) == (:w, :x,)
     end
 end
+