Skip to content
This repository
Browse code

Fix squeeze() for arrays with one item

  • Loading branch information...
commit 35d615ce930f48633020cc67b3cab4bc4bac621e 1 parent 2ad96bd
Tim Holy timholy authored

Showing 1 changed file with 8 additions and 4 deletions. Show diff stats Hide diff stats

  1. +8 4 base/abstractarray.jl
12 base/abstractarray.jl
@@ -152,10 +152,14 @@ reshape(a::AbstractArray, dims::Int...) = reshape(a, dims)
152 152 vec(a::AbstractArray) = reshape(a,max(size(a)))
153 153
154 154 function squeeze(A::AbstractArray)
155   - d = ()
156   - for i in size(A)
157   - if i != 1
158   - d = tuple(d..., i)
  155 + if length(A) == 1
  156 + d = (1,)
  157 + else
  158 + d = ()
  159 + for i in size(A)
  160 + if i != 1
  161 + d = tuple(d..., i)
  162 + end
159 163 end
160 164 end
161 165 reshape(A, d)

4 comments on commit 35d615c

Stefan Karpinski

I would argue that the old behavior was more correct.

Stefan Karpinski

I would also argue that almost any use of the squeeze function is probably a bug.

Stefan Karpinski

We should perhaps have a squeeze macro. I'll elaborate later.

Tim Holy
Collaborator

I'm sympathetic to the notion that squeeze is a bug, but if so we should delete it. I'll wait to hear your macro suggestion.

Please sign in to comment.
Something went wrong with that request. Please try again.