Repository: invenia/NamedDims.jl (julia)

Issue:
argmax and argmin
Any reason these aren't implemented?

e.g.

```
X = NamedDimsArray{(:a, :b)}(rand(2,2))
@assert argmax(X; dims=:a) == argmax(X; dims=1)
```

Pull request description:
Fixes #185

Patch:
diff --git a/src/functions.jl b/src/functions.jl
index e098dac..8b618fd 100644
--- a/src/functions.jl
+++ b/src/functions.jl
@@ -17,7 +17,7 @@ end
 
 # 1 Arg
 for (mod, funs) in (
-    (:Base, (:sum, :prod, :maximum, :minimum, :extrema)),
+    (:Base, (:sum, :prod, :maximum, :minimum, :extrema, :argmax, :argmin)),
     (:Statistics, (:mean, :std, :var, :median)),
 )
     for fun in funs
@@ -29,6 +29,19 @@ for (mod, funs) in (
     end
 end
 
+# 1 Arg, 2 Results
+for (mod, funs) in ((:Base, (:findmax, :findmin)),)
+    for fun in funs
+        @eval function $mod.$fun(a::NamedDimsArray; dims=:, kwargs...)
+            numerical_dims = dim(a, dims)
+            data = $mod.$fun(parent(a); dims=numerical_dims, kwargs...)
+            map(data) do item  # value and index
+                nameddimsarray_result(a, item, numerical_dims)
+            end
+        end
+    end
+end
+
 # 1 Arg - no default for `dims` keyword
 for (mod, funs) in ((:Base, (:cumsum, :cumprod, :sort, :sort!, :sortslices)),)
     for fun in funs


Test patch:
diff --git a/test/functions.jl b/test/functions.jl
index 62c59b7..5ce8d68 100644
--- a/test/functions.jl
+++ b/test/functions.jl
@@ -7,13 +7,21 @@ using Statistics
     a = [10 20; 31 40]
     nda = NamedDimsArray(a, (:x, :y))
 
-    @testset "$f" for f in (sum, prod, maximum, minimum, extrema)
+    @testset "$f" for f in (sum, prod, maximum, minimum, extrema, argmax, argmin)
         @test f(nda) == f(a)
         @test f(nda; dims=:x) == f(nda; dims=1) == f(a; dims=1)
 
         @test dimnames(f(nda; dims=:x)) == (:x, :y) == dimnames(f(nda; dims=1))
     end
 
+    @testset "$f" for f in (findmax, findmin)
+        @test f(nda) == f(a)
+        @test f(nda; dims=:x) == f(nda; dims=1) == f(a; dims=1)
+
+        @test dimnames(f(nda; dims=:x)[1]) == (:x, :y) == dimnames(f(nda; dims=1)[1])  # value
+        @test dimnames(f(nda; dims=:x)[2]) == (:x, :y) == dimnames(f(nda; dims=1)[2])  # index
+    end
+
     @testset "$f" for f in (cumsum, cumprod, sort, sortslices)
         @test f(nda; dims=:x) == f(nda; dims=1) == f(a; dims=1)