Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add squeeze with dims as keyword argument #528

Merged
merged 2 commits into from
Apr 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ Currently, the `@compat` macro supports the following syntaxes:

* `selectdim` to obtain a view of an array with a specified index for a specified dimension ([#26009]).

* `squeeze` with `dims` as keyword argument ([#26660]).

## Renaming

* `Display` is now `AbstractDisplay` ([#24831]).
Expand Down Expand Up @@ -618,3 +620,4 @@ includes this fix. Find the minimum version from there.
[#26369]: https://github.com/JuliaLang/julia/issues/26369
[#26436]: https://github.com/JuliaLang/julia/issues/26436
[#26442]: https://github.com/JuliaLang/julia/issues/26442
[#26660]: https://github.com/JuliaLang/julia/issues/26660
3 changes: 3 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1777,6 +1777,9 @@ if VERSION < v"0.7.0-DEV.4534"
reverse(a::AbstractArray; dims=nothing) =
dims===nothing ? Base.reverse(a) : Base.flipdim(a, dims)
end
if VERSION < v"0.7.0-DEV.4738"
Base.squeeze(A; dims=error("squeeze: keyword argument dims not assigned")) = squeeze(A, dims)
end

if !isdefined(Base, :selectdim) # 0.7.0-DEV.3976
export selectdim
Expand Down
7 changes: 7 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1673,6 +1673,13 @@ end
@test length(Compat.CartesianIndices((1:2,))) == 2
@test length(Compat.CartesianIndices((1:2, -1:1))) == 6

# 0.7.0-DEV.4738
@test squeeze([1 2], dims=1) == [1, 2]
@test_throws ArgumentError squeeze([1 2], dims=2)
@test_throws ArgumentError squeeze(hcat([1, 2]), dims=1)
@test squeeze(hcat([1, 2]), dims=2) == [1, 2]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add @test_throws ErrorException squeeze([1,2])?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It throws an UndefKeywordError on Julia master. Unless we want to add UndefKeywordError to Compat (which I'd say would be outside the scope of this PR), we can do @test_throws Exception squeeze([1,2]). Of course, that would mean the test would fail should there ever be a method added in base for squeeze(::AbstractArray), e.g. to automatically squeeze all singleton dimensions. I don't think this is going to happen. But if it does happen, the failing test would remind us to rework the squeeze definition here. So yes, I'll add that test.

@test_throws Exception squeeze([1,2])

# 0.7.0-DEV.3976
let A = rand(5,5)
@test selectdim(A, 1, 3) == A[3, :]
Expand Down